I'm working in a project which contains tests in mockito. I need to add more tests to this file, so I added a simple jqwik test, but if I try to run all tests all mockito ones are ignored.
"How can I use jqwik with Mockito?" is a common question. Here's a discussion of possible solutions. The most simple one:
class HelloTest {
@Mock private Logger logger;
@InjectMocks private MySubjectUnderTest sut;
private AutoCloseable mockitoCloseable;
@BeforeProperty //@BeforeTry
void initMocks() {
mockitoCloseable = MockitoAnnotations.openMocks(this);
}
@AfterProperty //@AfterTry
void closeMocks() throws Exception {
mockitoCloseable.close();
}
@Property
void testWithRandomData(@ForAll final String data) {
sut.doSomething();
// Verify whatever should happen to logger instance
// during doSomething() call, e.g.:
Mockito.verify(logger).log(Level.WARNING, "my message");
}
}
You go with @Before/AfterProperty
or with @Before/AfterTry
depending on whether your mocks should be reset for each try or for each property.