2014-10-18 24 views
0

我正在使用Jmockit的第一次,所以我可能會錯過一些微不足道的東西。我有一個測試方法addTopTenListsTest()它調用 - > mockObjectifyInstance.save()。實體(topTenList).now();但是,當我從Expectations()(嚴格期望值)(在下面的代碼塊中顯示)中註釋掉mockObjectifyInstance.save()調用時,測試用例仍然變爲綠色。我期待測試用例失敗,因爲將調用未嚴格期望中列出的模擬對象。當我沒有在嚴格的期望中提供正確的期望時,爲什麼我的JMockit測試用例不會失敗?

有什麼建議嗎?

@RunWith(JMockit.class) 
public class DataManagerTest { 

    @Mocked 
    ObjectifyService mockObjectifyServiceInstance; 

    @Mocked 
    Objectify mockObjectifyInstance; 

    @Test 
    public void addTopTenListsTest() { 

    final TopTenList topTenList = new TopTenList(); 

    new Expectations() { 
    { 

    ObjectifyService.ofy(); 
    result = mockObjectifyInstance; 

    // mockObjectifyInstance.save().entity(topTenList).now(); --> expected test case to fail when this is commented out 
    } 
    }; 

    DataManager datamanager = new DataManager(); 
    datamanager.addTopTenList(topTenList); 

    new Verifications() {{ 
    mockObjectifyInstance.save().entity(topTenList).now(); 
    }}; 
    } 
} 
+0

測試報告「缺少的調用:Objectity#保存()」增加了驗證碼給我。你能展示一個獨立的示例測試類,它可以被執行嗎? – 2014-10-20 14:34:41

+0

我弄清楚我做錯了什麼。我將添加它作爲答案。感謝您的期待! – 2014-10-23 13:57:34

回答

0

我想清楚我做錯了什麼。當使用嚴格的期望時,我仍然需要一個空的驗證塊,以便JMockIt知道驗證階段何時開始。

因此,如果我從「驗證」塊中刪除驗證,代碼將失敗。這意味着我可以在任何嚴格的斷言塊或驗證塊

@RunWith(JMockit.class) 
public class DataManagerTest { 

    @Mocked 
    ObjectifyService mockObjectifyServiceInstance; 

    @Mocked 
    Objectify mockObjectifyInstance; 

    @Test 
    public void addTopTenListsTest() { 

    final TopTenList topTenList = new TopTenList(); 

    new Expectations() { 
    { 

     ObjectifyService.ofy(); 
     result = mockObjectifyInstance; 
    // mockObjectifyInstance.save().entity(topTenList).now(); --> expected test case to fail when this is commented out 
    } 
}; 

DataManager datamanager = new DataManager(); 
datamanager.addTopTenList(topTenList); 

new Verifications() {{ 
    //leave empty for the strict assertion to play in! 
}}; 
} 

}

相關問題