2014-02-20 42 views
3

這就奇怪了:我使用的是RuleJUnitRuleMockery了一段時間,它總是完全工作正常使用JUnitJMock:其中在測試結束時檢查的期望,如果一個(或更多)缺少測試失敗。然而,這個片段是不是在Eclipse工作對我來說(DAOorg.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的行爲不如預期,而相同的代碼工作(測試失敗)?

+1

檢查您的Eclipse構建路徑的確保你有相同的依賴有你的build.gradle文件。 – jeremyjjbrown

+0

謝謝,但這並不是問題所在。檢查http://s25.postimg.org/luflrf533/eclipse_path.png和http://s25.postimg.org/9v43k3zi7/eclipse_path_2.png – ThanksForAllTheFish

回答

1

我終於找到了問題。
這個問題與他的評論中建議的類路徑相關爲jeremyjjbrown,但與Eclipse-Gradle plugin及其導入項目的方式有關。
我如何解決這個問題

  1. 在Eclipse
  2. 刪除的項目去了終端,在項目源文件夾
  3. 類型gradle cleanEclipse
  4. 類型gradle eclipse在Eclipse
  5. ,進口該項目繼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插入到classpath中。更改testCompile

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

真正解決了這個問題。

我感謝pbanfi誰親自幫助我調試和解決問題的真正