EasyMock:我做错了什么?
所以,我需要测试一个应用程序的服务层(我需要测试一些方法——这是我第一次接触测试部分)
public void testGetAllOrderedDescByRating() {
FAQ faq1 = initFAQ(new FAQ(), 5, 1L);
FAQ faq2 = initFAQ(new FAQ(), 3, 2L);
FAQ faq3 = initFAQ(new FAQ(), 11, 3L);
EasyMock.expect(faqDao.getAllOrderedDescByRating()).andReturn(
new ArrayList<FAQ>());
EasyMock.expect(faqDao.makePersistent((FAQ) EasyMock.anyObject()))
.andReturn(new FAQ());
EasyMock.replay(faqDao);
FAQService.saveFAQ(faq1);
FAQService.saveFAQ(faq2);
FAQService.saveFAQ(faq3);
List<FAQ> list = FAQService.getAllOrderedDescByRating();
Assert.assertEquals(list.get(0).getRating(), 11.0);
Assert.assertEquals(list.get(1).getRating(), 5.0);
Assert.assertEquals(list.get(2).getRating(), 3.0);
EasyMock.verify(faqDao);
}
来自接口的方法:
列表 getAllOrderedDescByRating();
我收到:
java.lang.AssertionError:
意外的方法调用 makePersistent(faq.FAQ@3461d1): getAllOrderedDescByRating():预期:1,实际:0 makePersistent():预期:1,实际:1 (+1)
我做错了什么?
So, I need to test the Service Layer for an application (I need to test some methods - this is my first contact with the testing section)
public void testGetAllOrderedDescByRating() {
FAQ faq1 = initFAQ(new FAQ(), 5, 1L);
FAQ faq2 = initFAQ(new FAQ(), 3, 2L);
FAQ faq3 = initFAQ(new FAQ(), 11, 3L);
EasyMock.expect(faqDao.getAllOrderedDescByRating()).andReturn(
new ArrayList<FAQ>());
EasyMock.expect(faqDao.makePersistent((FAQ) EasyMock.anyObject()))
.andReturn(new FAQ());
EasyMock.replay(faqDao);
FAQService.saveFAQ(faq1);
FAQService.saveFAQ(faq2);
FAQService.saveFAQ(faq3);
List<FAQ> list = FAQService.getAllOrderedDescByRating();
Assert.assertEquals(list.get(0).getRating(), 11.0);
Assert.assertEquals(list.get(1).getRating(), 5.0);
Assert.assertEquals(list.get(2).getRating(), 3.0);
EasyMock.verify(faqDao);
}
The method from the interface:
List getAllOrderedDescByRating();
I receive:
java.lang.AssertionError:
Unexpected method call
makePersistent(faq.FAQ@3461d1):
getAllOrderedDescByRating(): expected: 1, actual: 0
makePersistent(): expected: 1, actual: 1 (+1)
What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您执行了 EasyMock 看到的 3 个 saveFAQ 调用,但您没有告诉它。您调用的 FAQService 有可能连接到您的 faqDao 吗?
我希望您将 3 个 FAQ 项目添加到您返回的 ArrayList 中,而不是返回一个空项目,并且根本不需要调用 saveFAQ() 方法(因此删除对它的三个调用)。
It looks like your doing 3 saveFAQ calls that EasyMock sees but that you have not told it about. Any chance the FAQService you call is wired to your faqDao?
I would expect you would have added the 3 FAQ items to an ArrayList you return instead of returning an empty one, and that there would be no need to call the saveFAQ() method at all (so remove the three calls to it).