UTBotJava icon indicating copy to clipboard operation
UTBotJava copied to clipboard

Improve Spring unit tests generation for classes with non-injected fields

Open EgorkaKulikov opened this issue 3 years ago • 1 comments

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:

  1. What if the constructor contains some non-injectible field?
class A(List<int> ints)
  1. What if the constructor contains two fields of same non-injectible type?
class A(List<int> ints1, List<int> ints2)
  1. 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)
  1. 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)

EgorkaKulikov avatar Jun 29 '23 08:06 EgorkaKulikov

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.

IlyaMuravjov avatar Jul 03 '23 12:07 IlyaMuravjov