UTBotJava
UTBotJava copied to clipboard
Improve Spring unit tests generation for classes with non-injected fields
Description
Currently we use the following approach in unit tests generation.
The code
class A(
B b
C c
methodUnderTest()
}
is converted to
class TestClass {
@InjectMocks A a
@Mock B b
@Mock C c
}
and a mapping from type to the list of models is used B -> m11, m12, ... C -> m21, m22, ...
There are some possible problems in this approach:
- What if the constructor contains some non-injectible field?
class A(List<int> ints)
- What if the constructor contains two fields of same non-injectible type?
class A(List<int> ints1, List<int> ints2)
- Is it possible to create some util class (may be nested or inner), that does not use Spring and has the following constructor
class Utils(B b1, B b2)
- Is it a used practice to pass to constructor some non-injectible type with injectible ones? What should we do?
class A(B b, List<int> ints)
I think, it's worth noting that since we are talking about unit tests, here so called injected fields are equivalent to mocked fields.
I think unit test code generation should be tested on classes with the following constructors:
public ClassUnderTest(Random random, int someNumber) { ... }
public ClassUnderTest(Random random1, Random random2) { ... }
Here, I used Random as type that should get mocked and int as type that shouldn't get mocked.