-
Notifications
You must be signed in to change notification settings - Fork 8
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
Better Mockito integration #1
Comments
Right now, you can save some boilerplate by using MockitoAnnotations.initMocks: import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.specnaz.junit.SpecnazJUnit;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class UsingMockitoSpec extends SpecnazJUnit {
@Mock
List<String> list;
{
describes("A Mockito test", it -> {
it.beginsEach(() -> {
MockitoAnnotations.initMocks(this);
});
it.should("initialize the mocks correctly", () -> {
assertThat(list.get(99)).isNull();
});
});
}
} Whatever design we come up with, I would assume it would do something very similar to above. The question then becomes, is it worth to develop a feature just to save one Another thing is that there might be legitimate cases when you want to init the mocks in a Pros of developing this solution:
Cons:
If we do choose to implement this feature, probably the MockitoSession API would come in handy. |
This has been done, but in a rather different way than I originally planned. It's possible thanks to the JUnit Rules support that has been added in version Example: public class MockitoExampleSpec extends SpecnazJUnit {
public Rule<MockitoRule> mockitoRule = Rule.of(() -> MockitoJUnit.rule());
@Mock
private List<Integer> listMock;
{
describes("Using the JUnit Mockito Rule in Specnaz", it -> {
it.should("initialize fields annotated with @Mock", () -> {
when(listMock.get(0)).thenReturn(400 + 56);
assertEquals(456, (int) listMock.get(0));
});
});
}
} Because of that, I'm resolving this one. |
In "regular" JUnit, you can use the MockitoJUnitRunner to avoid explicitly initializing mocks (among other things - see the JavaDocs linked above for details):
There was a Feature Request to add this capability to Specnaz.
The text was updated successfully, but these errors were encountered: