This is strange: I am using JUnit
with JMock
using the Rule
JUnitRuleMockery for a while and it always worked perfectly fine: expectations where checked at the end of the test and if one (or more) was missing the test was failing. However, this snippet is not working for me in Eclipse (DAO
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"));
//context.assertIsSatisfied();
}
When I say "not working" I mean that I have to uncomment the line context.assertIsSatisfied();
to see the test failing (when in Eclipse I run this class as a Junit test). For completeness, this is the code service.getValue
, where findOne
is clearly called only once:
public String getValue (String cobrandName, String property)
{
Cobrand cobrand = cobrandDAO.findOne("cobrand", cobrandName);
return "value";
}
I am using Gradle
to manage my build, and if I execute the command gradle clean test
, with the line context.assertIsSatisfied();
commented, the test fails. Here my 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'
}
What am I doing wrong? What can I do to check why Run As
-> JUnit test
in Eclipse is not behaving as expected while the same code works (the test fails) when executed via gradle clean test
?