Closed
Description
Description
Consider unit tests generation for the following class
@Service
public class OrderService {
@Autowired
private List<Order> lst;
public Integer getOrdersSize() {
return lst.size();
}
}
Expected behaviour
The correct test should look like this manually written one:
public final class OrderServiceTest {
@InjectMocks
private OrderService orderService;
@Spy
private ArrayList<Order> ordersSpy;
@Test
public void testGetOrdersSize_IntegerValueOf() throws Exception {
Order orderMock = mock(Order.class);
ordersSpy.add(orderMock);
Integer actual = orderService.getOrdersSize();
Integer expected = 1;
assertEquals(expected, actual);
}
// Some code to open and close mocks
}
Context
One of potential difficulties is to distinguis autowired and non-autowired collections to decide, if have to create test class field with @Spy
annotation. After that, we should add all created elements into already created spied variable.
See for more details
https://stackoverflow.com/questions/42351117/mockito-injecting-a-list-of-mocks
https://www.baeldung.com/mockito-spy
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done