It's strange: I use JUnitto JMockusing Rule JUnitRuleMockery some time, and it's always worked perfectly: the expectations that have been tested at the end of the test, and if one (or more) was absent, the test was unsuccessful. However, this snippet does not work for me in Eclipse ( DAO- this is the interface described in org.mongodb.morphia.dao.DAO):
@Rule public JUnitRuleMockery context = new JUnitRuleMockery();
private DAO<Cobrand, ObjectId> dao = context.mock(DAO.class);
private final Retriever service = new Retriever(dao);
@Test
public void canRetrieveASinglePropertyValue () throws NoSuchFieldException, IllegalAccessException
{
context.checking(new Expectations()
{
{
oneOf(dao).findOne("cobrand", "cobrandName"); will(returnValue(prepareFakeCobrand("cobrandName")));
oneOf(dao).findOne("cobrand", "cobrandName"); will(returnValue(prepareFakeCobrand("cobrandName")));
}
});
String value = service.getValue("cobrandName", "property");
assertThat(value, equalTo("value"));
}
When I say "does not work", I mean that I need to uncomment the line context.assertIsSatisfied();to see the test lost (when in Eclipse I run this class as a Junit test). For completeness, this is code service.getValuewhere it is findOneexplicitly called only once:
public String getValue (String cobrandName, String property)
{
Cobrand cobrand = cobrandDAO.findOne("cobrand", cobrandName);
return "value";
}
Gradle , gradle clean test, context.assertIsSatisfied();, . build.gradle
dependencies {
def hamcrestVersion = "1.3"
def jmockVersion = "2.6.0"
compile 'org.mongodb.morphia:morphia:0.106'
testCompile "org.hamcrest:hamcrest-core:${hamcrestVersion}"
testCompile "org.hamcrest:hamcrest-library:${hamcrestVersion}"
testCompile "org.jmock:jmock:${jmockVersion}"
testCompile "org.jmock:jmock-junit4:${jmockVersion}"
testCompile 'junit:junit:4.11'
}
? , , Run As → JUnit test Eclipse , , ( ) gradle clean test?