Comments (2)
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 beginsEach
method? (I don't know; this is not a sarcastic question, I'm honestly curious whether it's a good trade-off).
Another thing is that there might be legitimate cases when you want to init the mocks in a beginsAll
instead of a beginsEach
- the implemented solution would have to allow you to switch between the 2 modes (I would most likely make the beginsEach
behavior the default).
Pros of developing this solution:
- save a little boilerplate
Cons:
- introduce some (potentially) surprising behavior (it might not be obvious when and how are the mocks being initialized)
- increased implementation complexity
If we do choose to implement this feature, probably the MockitoSession API would come in handy.
from specnaz.
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 1.3
.
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.
from specnaz.
Related Issues (10)
- Specnaz TestNG runner fails, by trying to run *describes lambdas as test (and failing at it) HOT 3
- When we get an unexpected exception Specnaz should print full message/stacktrace for it HOT 6
- Problem with Maven HOT 3
- formatParamsDesc not sanitizing input HOT 3
- Support tests expecting Exceptions HOT 2
- Top-level description missing from terminal output when running tests HOT 2
- Fail on no tests found? HOT 2
- Support for top-level `fdescribes`? HOT 3
- Testing multiplatform code? HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from specnaz.