需要编写测试帮助

发布于 2024-09-03 04:53:51 字数 968 浏览 4 评论 0原文

我正在尝试为这个名为 Receiver 的类编写一个测试:

public void get(People person) {
            if(null != person) {
               LOG.info("Person with ID " + person.getId() + " received");
               processor.process(person);
             }else{
              LOG.info("Person not received abort!");   
              }
        }

这是测试:

@Test
    public void testReceivePerson(){
        context.checking(new Expectations() {{          
            receiver.get(person);
            atLeast(1).of(person).getId();
            will(returnValue(String.class));        
        }});
    }

注意:接收器是 Receiver 类的实例(真实而不是模拟),处理器是 Processor 类的实例(真实的而不是模拟的)处理人(People 类的模拟对象)。 GetId 是一个 String 而不是 int 方法,没有错误。

测试失败:意外调用 person.getId()

我正在使用 jMock 任何帮助将不胜感激。据我了解,当我调用此 get 方法来正确执行它时,我需要模拟 person.getId() ,而且我已经绕了一段时间了任何帮助将不胜感激。

I'm trying to write a test for this class its called Receiver :

public void get(People person) {
            if(null != person) {
               LOG.info("Person with ID " + person.getId() + " received");
               processor.process(person);
             }else{
              LOG.info("Person not received abort!");   
              }
        }

Here is the test :

@Test
    public void testReceivePerson(){
        context.checking(new Expectations() {{          
            receiver.get(person);
            atLeast(1).of(person).getId();
            will(returnValue(String.class));        
        }});
    }

Note: receiver is the instance of Receiver class(real not mock), processor is the instance of Processor class(real not mock) which processes the person(mock object of People class). GetId is a String not int method that is not mistake.

Test fails : unexpected invocation of
person.getId()

I'm using jMock any help would be appreciated. As I understood when I call this get method to execute it properly I need to mock person.getId() , and I've been sniping around in circles for a while now any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

马蹄踏│碎落叶 2024-09-10 04:53:52

另外,您应该使用 allowed() 而不是 atLeast(1),因为您在这里存根了 person 对象。最后,如果 Person 只是一个值类型,那么最好只使用该类。

Also, you should use allowing() instead of atLeast(1), since you're stubbing the person object here. Finally, if Person is just a value type, it might be better to just use the class.

梨涡 2024-09-10 04:53:51

如果我理解正确,你必须移动 receive.get(person); 行在 context.checking 的范围之后 - 因为这是测试的执行而不是设置期望。所以尝试一下这个:

@Test
public void testReceivePerson(){
    context.checking(new Expectations() {{          
        atLeast(1).of(person).getId();
        will(returnValue(String.class));        
    }});
    receiver.get(person);
}

If I understand correctly, you have to move the line receiver.get(person); after the scope of context.checking - because this is the execution of your test and not setting expectation. So give this one a go:

@Test
public void testReceivePerson(){
    context.checking(new Expectations() {{          
        atLeast(1).of(person).getId();
        will(returnValue(String.class));        
    }});
    receiver.get(person);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文