我第一次使用Jmockit所以我可能会遗漏一些微不足道的东西。 我有一个测试方法 addTopTenListsTest(),它调用 - > 。mockObjectifyInstance.save()实体(topTenList)。现在();
Objectify被嘲笑,但是当我从Expectations()(Strict Expectation)中注释掉mockObjectifyInstance.save()调用时(在下面的代码块中显示),测试用例仍然变为绿色。我期待测试用例失败,因为将对未在严格期望中列出的模拟对象进行调用。
有什么建议吗?
@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 :(得分: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!
}};
}
}