Skip to content

Uncompilable code is generated for Spring class using Optional #2065

Closed
@EgorkaKulikov

Description

@EgorkaKulikov

To Reproduce

Try to generate tests for the following method from open-source project spring-boot-testing-main:

public Double getOrderPriceById(long id) throws Exception {
        var order = orderRepository.findById(id);
        if (order == null) {
            throw new Exception("Order id is incorrect");
        }

        return order.get().getPrice();
    }

Method findById has the following signature Optional<T> findById(ID id);

Expected behavior

Generated tests may be compiled successfully.

Actual behavior

Two of three generated tests starts with the following line, where optional variable was never declared.

(when(orderRepositoryMock.findById(any()))).thenReturn(optional);

Additional context

Yuri recommends to ensure that we call setupInstrumentation properly and to find problem around it.

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