简体   繁体   English

EasyMock:我做错了什么?

[英]EasyMock: What I'm doing wrong?

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(); 列出getAllOrderedDescByRating();

I receive: 我收到:

java.lang.AssertionError: java.lang.AssertionError:
Unexpected method call makePersistent(faq.FAQ@3461d1): getAllOrderedDescByRating(): expected: 1, actual: 0 makePersistent(): expected: 1, actual: 1 (+1) 意外的方法调用makePersistent(faq.FAQ@3461d1):getAllOrderedDescByRating():预期:1,实际:0 makePersistent():预期:1,实际:1(+1)

What I'm doing wrong? 我做错了什么?

It looks like your doing 3 saveFAQ calls that EasyMock sees but that you have not told it about. 看起来您在进行EasyMock看到的3个saveFAQ调用,但是您没有告诉过它。 Any chance the FAQService you call is wired to your faqDao? 您致电的FAQService是否有可能连接到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). 我希望您将3个FAQ项添加到返回的ArrayList中,而不是返回一个空的ArrayList,并且根本不需要调用saveFAQ()方法(因此删除对它的三个调用)。

List<FAQ> l = new ArrayList<FAQ>();
FAQ faq1 = initFAQ(new FAQ(), 5, 1L);
l.add(faq1);
FAQ faq2 = initFAQ(new FAQ(), 3, 2L);
l.add(faq2);
FAQ faq3 = initFAQ(new FAQ(), 11, 3L);
l.add(faq3);

EasyMock.expect(faqDao.getAllOrderedDescByRating()).andReturn(l);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM