【发布时间】:2014-03-21 21:15:18
【问题描述】:
这很奇怪:我使用 JMock 和 JMock 有一段时间了,它总是工作得很好:在测试结束时检查期望值,如果有一个(或更多) 丢失了测试失败。但是,这个 sn-p 在 Eclipse 中对我不起作用(DAO 是 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 显然只调用了一次:
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'
}
我做错了什么?我可以做些什么来检查为什么 Eclipse 中的 Run As -> JUnit test 在通过 gradle clean test 执行相同的代码工作(测试失败)时没有按预期运行?
【问题讨论】:
-
检查你的 Eclipse 构建路径,确保你有与 build.gradle 文件相同的依赖项。
标签: java eclipse junit gradle jmock