Skip to content

Failing Spring unit test is generated (possibly due to incorrect injection) #2386

@EgorkaKulikov

Description

@EgorkaKulikov

To Reproduce

Consider generating unit tests for project spring-boot-testing with modified OrderService class.

@Service
public class OrderService {

    @Autowired
    private OrderRepository orderRepository;

    private List<Order> baseOrders = new ArrayList<>();

    public int getOrdersCount() { return orderRepository.findAll().size() + baseOrders.size(); }
}

Expected behavior

Happy pass test is generated with correct assertions.

Actual behavior

The following failing test is generated.
It fails because real number of items in collection is 3, not 6.

@Test
    @DisplayName("getOrdersCount: OrderRepositoryFindAll -> return orderRepository.findAll().size() + baseOrders.size()")
    public void testGetOrdersCount_ListSize() {
        ArrayList arrayList1 = new ArrayList();
        arrayList1.add(null);
        arrayList1.add(null);
        arrayList1.add(null);
        (when(orderRepositoryMock.findAll())).thenReturn(arrayList1);

        int actual = orderService.getOrdersCount();

        assertEquals(6, actual);
    }

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorcomp-springIssue is related to Spring projects supportctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions