Skip to content

Make Spring-specific no NPE speculation to only trigger for mocks #2572

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

Closed
wants to merge 1 commit into from

Conversation

IlyaMuravjov
Copy link
Collaborator

Description

In Spring-specific unit tests class under test field can't be mocks in one execution and null in another due to @InjectMock either always injecting or non injecting the mocks into class under test.

How to test

Manual tests

  • Open spring-boot-testing project.
  • Generate unit tests using Mock everything outside the class strategy for the following classes.

There should be NPE tests for equals() in the following class:

@Getter
@Setter
@Builder
@ToString
@Jacksonized
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EqualsAndHashCode
@Table(name = "orders")
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @Email
    @NotEmpty
    String buyer;
    Double price;
    int qty;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || !(o instanceof Order)) return false;
        Order order = (Order) o;
        return qty == order.qty && id.equals(order.id) && buyer.equals(order.buyer) && price.equals(order.price);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, buyer, price, qty);
    }
}

There should be no NPE tests for createOrder() in the following class

@Service
public class OrderService {
    private final OrderRepository orderRepository;

    public Order createOrder(Order order) {
        return orderRepository.save(order);
    }
}

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 comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug-fix PR is fixing a bug comp-spring Issue is related to Spring projects support labels Aug 31, 2023
@IlyaMuravjov
Copy link
Collaborator Author

On second thought we can't rely solely on the fact that engine decided to mock or not to mock some fields, since fuzzer may do mocking differently, so I think we should do some execution filtering after the generation has completed.

@IlyaMuravjov IlyaMuravjov force-pushed the ilya_m/spring-non-null-mocks branch 2 times, most recently from 7f7a168 to 1d1864d Compare August 31, 2023 16:10
@IlyaMuravjov
Copy link
Collaborator Author

Replaced with #2617

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-spring Issue is related to Spring projects support comp-symbolic-engine Issue is related to the symbolic execution engine ctg-bug-fix PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant