本文介绍了JUnit,JMock,JUnitRuleMockery:我缺少什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪:我正在使用RuleJMock一起使用JUnitJMock JUnitRuleMockery 一段时间,它始终运行良好:在测试结束时检查了期望值,如果一个或多个(或多个)缺失,则测试失败.但是,此代码段在Eclipse中对我不起作用(DAOorg.mongodb.morphia.dao.DAO中描述的接口):

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();
  }

当我说不起作用"时,我的意思是我必须取消注释行context.assertIsSatisfied();才能看到测试失败(在Eclipse中,我将该类作为Junit测试运行).为了完整起见,这是代码service.getValue,其中findOne显然仅被调用一次:

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";
}

我正在使用Gradle来管理我的构建,并且如果执行命令gradle clean test,并注释了行context.assertIsSatisfied();,则测试将失败.这是我的build.gradle

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'
}

我做错了什么?我该怎么做,以检查为什么通过gradle clean test执行相同的代码(测试失败)时,Eclipse中的Run As-> JUnit test表现不正常?

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?

推荐答案

我终于找到了问题.
该问题确实与类路径有关,如他的评论中建议的 jeremyjjbrown 一样,但与 Eclipse-Gradle插件及其导入项目的方式.
导入项目

  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

这确实解决了问题,但是后来我无法直接在Eclipse内部管理Gradle(即,我无法right click-> Gradle-> Refresh All),因为该项目没有Gradle性质.将其转换为Gradle项目后,原来的问题又回来了.所以,最后,

我如何真正解决问题
看着我的gradle.build,您会看到testCompile "org.jmock:jmock-junit4:2.6.0". jMock有一个微妙的问题:它带来了junit-dep:4.4. Eclipse中的这种依赖关系导致了问题,将相关的jar插入了类路径.将testCompile行更改为

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"
}

真正解决了这个问题.

我感谢 pbanfi 亲自帮助我调试并真正解决了问题

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

这篇关于JUnit,JMock,JUnitRuleMockery:我缺少什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:10