這就奇怪了:我使用的是Rule
JUnitRuleMockery了一段時間,它總是完全工作正常使用JUnit
與JMock
:其中在測試結束時檢查的期望,如果一個(或更多)缺少測試失敗。然而,這個片段是不是在Eclipse工作對我來說(DAO
是org.mongodb.morphia.dao.DAO
所描述的接口):的JUnit,JMock的,JUnitRuleMockery:我缺少什麼
@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
顯然只調用一次:我用Gradle
來管理我的構建
public String getValue (String cobrandName, String property)
{
Cobrand cobrand = cobrandDAO.findOne("cobrand", cobrandName);
return "value";
}
,如果我執行與註釋行context.assertIsSatisfied();
命令gradle clean test
,測試失敗。這裏我的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
的行爲不如預期,而相同的代碼工作(測試失敗)?
檢查您的Eclipse構建路徑的確保你有相同的依賴有你的build.gradle文件。 – jeremyjjbrown
謝謝,但這並不是問題所在。檢查http://s25.postimg.org/luflrf533/eclipse_path.png和http://s25.postimg.org/9v43k3zi7/eclipse_path_2.png – ThanksForAllTheFish