Skip to content

One branch is not covered on function with Optionals #2092

Open
@EgorkaKulikov

Description

@EgorkaKulikov

To Reproduce

Consider the following class under test

import java.util.OptionalInt;

public class OrderService {
    private final OrderRepository orderRepository;

    public OrderService(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    public int methodUnderTest(int id) {
        OptionalInt res = orderRepository.findById(id);
        if (res == null || res.isEmpty())  {
            return 1;
        } else {
            return 2;
        }
    }
}

Another mentioned class looks as follows:

import java.util.OptionalInt;

public class OrderRepository {
    public OptionalInt findById(int id) {
        if (id > 0) {
            return OptionalInt.of(id);
        } else {
            return null;
        }
    }
}

Generate tests with

  • mock strategy mock everything outside class
  • 100% of Symbolic engine

Expected behavior

Two tests for both branches of method under test were generated.

Actual behavior

Only one test was generated:

@Test
public void testMethodUnderTest1() {
    OrderRepository orderRepositoryMock = mock(OrderRepository.class);
    OptionalInt optionalInt = empty();
    (when(orderRepositoryMock.findById(anyInt()))).thenReturn(optionalInt);
    OrderService orderService = new OrderService(orderRepositoryMock);

    int actual = orderService.methodUnderTest(0);

    assertEquals(1, actual);
}

Additional context

Replacing OptionalInt with Optional<Integer> does not change the behavior.

Metadata

Metadata

Assignees

Labels

comp-symbolic-engineIssue is related to the symbolic execution enginectg-bugIssue is a bug

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions