Skip to content

Consider dynamically created mocks when pre minimizing fuzzer output and properly initialize @Mock fields #2569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2023

Conversation

IlyaMuravjov
Copy link
Collaborator

Description

When fuzzer executions are pre minimized we should consider mocks that we were added during concrete executions for methods that were actually invoked, but whose behavior wasn't defined by the fuzzer itself.

Also, makes fixes field initialization (when creating @InjectMock field we should not mark @Mock fields as initialized).

How to test

Manual tests

Add the following method to OrderService in spring-boot-testing project and generate unit tests, using "Mock everything outside the class" mock strategy and 100% fuzzing.

public double getTotalPrice() {
    return orderRepository.findAll().stream().mapToDouble(Order::getPrice).sum();
}

There should be test with real list:

public void testGetTotalPriceReturnsZero() {
    List list = emptyList();
    (when(orderRepositoryMock.findAll())).thenReturn(list);

    double actual = simpleOrderService.getTotalPrice();

    assertEquals(0.0, actual, 1.0E-6);
}

There should be no tests with mocked lists:

public void testGetTotalPriceReturnsZero() {
    List listMock = mock(List.class);
    Stream streamMock = mock(Stream.class);
    DoubleStream doubleStreamMock = mock(DoubleStream.class);
    (when(doubleStreamMock.sum())).thenReturn(0.0);
    (when(streamMock.mapToDouble(any()))).thenReturn(doubleStreamMock);
    (when(listMock.stream())).thenReturn(streamMock);
    (when(orderRepositoryMock.findAll())).thenReturn(listMock);

    double actual = simpleOrderService.getTotalPrice();

    assertEquals(0.0, actual, 1.0E-6);
}

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@IlyaMuravjov IlyaMuravjov added ctg-enhancement New feature, improvement or change request comp-codegen Issue is related to code generator comp-minimizer Issue is related to Minimization phase ctg-bug-fix PR is fixing a bug comp-spring Issue is related to Spring projects support labels Aug 30, 2023
@IlyaMuravjov IlyaMuravjov changed the title Consider dynamically created mocks when pre minimizing fuzzer output Consider dynamically created mocks when pre minimizing fuzzer output and properly initialize @Mock fields Aug 30, 2023
@EgorkaKulikov EgorkaKulikov merged commit eb8ece9 into main Aug 31, 2023
@EgorkaKulikov EgorkaKulikov deleted the ilya_m/minimize-dynamic-mocks branch August 31, 2023 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator comp-minimizer Issue is related to Minimization phase comp-spring Issue is related to Spring projects support ctg-bug-fix PR is fixing a bug ctg-enhancement New feature, improvement or change request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants