|
| 1 | +# UtUtils class |
| 2 | + |
| 3 | +## What are the utility methods |
| 4 | + |
| 5 | +_Utility methods_ implement common, often re-used operations which are helpful for accomplishing tasks in many |
| 6 | +classes. In UnitTestBot _utility methods_ include those related to creating instances, checking deep |
| 7 | +equals, working with arrays, using lambdas and so on — miscellaneous methods necessary for generated tests. |
| 8 | + |
| 9 | +## Why to create UtUtils class |
| 10 | + |
| 11 | +Previously UnitTestBot generated _utility methods_ for each test class when they were needed — and only those which were necessary for the given class. They were declared right in the generated test class, occupying space. Generating multiple test classes often resulted in duplicating _utility methods_ and consuming even more space. |
| 12 | + |
| 13 | +For now UnitTestBot provides a special `UtUtils` class containing all _utility methods_ if at least one test class needs some of them. This class is generated once and the specific methods are imported from it if necessary. No need for _utility methods_ — no `UtUtils` class is generated. |
| 14 | + |
| 15 | +## What does it look like |
| 16 | + |
| 17 | +Here is an example of a comment inherent to every `UtUtils` class: |
| 18 | + |
| 19 | +<img width="494" alt="ututils" src="https://user-images.githubusercontent.com/64418523/196719780-2603f141-e922-40fc-9a0a-533aaacc5c49.png"> |
| 20 | + |
| 21 | +As one can see, the comment mentions two characteristics of the `UtUtils` class: |
| 22 | + |
| 23 | +1. _Version_ |
| 24 | + |
| 25 | +If the generated tests require additional _utility methods_, the |
| 26 | +existing `UtUtils` class is upgraded and gets a new version number, which should be defined here: |
| 27 | + |
| 28 | +`org.utbot.framework.codegen.model.constructor.builtin.UtilClassFileMethodProvider.UTIL_CLASS_VERSION` |
| 29 | + |
| 30 | +_2. Mockito support_ |
| 31 | + |
| 32 | +UnitTestBot uses [Mockito framework](https://site.mockito.org/) to implement mocking. When generated tests imply mocking, the |
| 33 | +`deepEquals()` |
| 34 | +_utility method_ should be configured — it should have a check: whether the compared object is a mock or not. That is why the `UtUtils` class for the tests with mocking differs from the one without mocking support. |
| 35 | + |
| 36 | +If you have previously generated tests with mocking, the next `UtUtils` class will support mocking as well — even if |
| 37 | +it is upgraded or current tests do not need mocking, so that the existing tests can still |
| 38 | +rely on the proper methods from `UtUtils` class. |
| 39 | + |
| 40 | +## Where to find it |
| 41 | + |
| 42 | +`UtUtils` class is usually located in the chosen **Test sources root** near the generated test classes. |
| 43 | + |
| 44 | +## How to test |
| 45 | + |
| 46 | +If you want to test `UtUtils` class generation using UnitTestBot project itself, refer to the [Manual testing of |
| 47 | +UtUtils class generation #1233](https://github.com/UnitTestBot/UTBotJava/issues/1233). |
| 48 | + |
| 49 | +## How to improve |
| 50 | + |
| 51 | +UnitTestBot does not currently support generating tests for classes from multiple modules simultaneously. If this option was possible, we would probably have to generate a separate `UtUtils` class for each module. Perhaps we could find a special location for a `UtUtils` class reachable from every module. |
| 52 | + |
| 53 | +For now, you can generate separate `UtUtils` classes for different modules only if you manually choose the different **Test sources roots** when generating tests. |
0 commit comments