JUnit, JMock, JUnitRuleMockery: what am I missing

前提是你 提交于 2019-12-23 16:53:43

问题


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?


回答1:


I finally found the problem.
The issue did was related to classpath as jeremyjjbrown suggested in his comment, but was related to the Eclipse-Gradle plugin and the way it imports projects.
How I solved the problem

  1. deleted the project in Eclipse
  2. went to terminal, in the project source folder
  3. typed gradle cleanEclipse
  4. typed gradle eclipse
  5. in Eclipse, import the project following the Import -> Existing Projects into Workspace

That indeed solved the problem, but then I could not manage Gradle directly inside Eclipse (i.e. I could not right click -> Gradle -> Refresh All) since the project did not have a Gradle nature. Converting it to a Gradle project made the original problem coming back. So, finally,

How I really solved the problem
Looking at my gradle.build, you can see testCompile "org.jmock:jmock-junit4:2.6.0". jMock has a subtle problem: it brings junit-dep:4.4 with it. That dependency in Eclipse was causing the problem, inserting the related jar into the classpath. Changing the testCompile line to

testCompile ("org.jmock:jmock-junit4:${jmockVersion}") {
  exclude group:"junit"
}

really solved the problem.

I thank pbanfi who personally helped my debugging and solving the problem for real



来源:https://stackoverflow.com/questions/21916100/junit-jmock-junitrulemockery-what-am-i-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!