【问题标题】:JMock, Getting errorJMock,出现错误
【发布时间】:2013-10-09 07:52:44
【问题描述】:

出现错误:java.lang.AssertionError:意外调用:user.setUserName("John") 没有指定期望:你... - 忘记以基数从句开始期望? - 调用模拟方法来指定期望的参数? 在此之前发生了什么:什么都没有! 在 org.jmock.api.ExpectationError.unexpected(ExpectationError.java:23)

代码:

Mockery context = new JUnit4Mockery();

@Test
public void testSayHello(){
    context.setImposteriser(ClassImposteriser.INSTANCE);
    final User user =  context.mock(User.class);
//  user.setUserName("John");
    context.checking(new Expectations(){{
        exactly(1).of(user);
        user.setUserName("John");
        will(returnValue("Hello! John"));
    }}
    );
     context.assertIsSatisfied();
    /*HelloWorld helloWorld = new HelloWorld();
    Assert.assertEquals(helloWorld.sayHelloToUser(user), "Hello! John");
    ;*/
}

【问题讨论】:

    标签: java jmock


    【解决方案1】:

    设置期望时,您可以通过在调用 of() 返回的对象上调用该方法来指定要模拟的方法,而不是在模拟本身上(这不是 EasyMock)。

    @Test
    public void testSayHello(){
        // setting up the mock
        context.setImposteriser(ClassImposteriser.INSTANCE);
        final User user =  context.mock(User.class);
        context.checking(new Expectations(){{
            exactly(1).of(user).setUserName("John");
            will(returnValue("Hello! John"));
        }});
    
        // using the mock
        HelloWorld helloWorld = new HelloWorld();
        String greeting = helloWorld.sayHelloToUser(user);
    
        // checking things afterward
        Assert.assertEquals(greeting, "Hello! John");
        context.assertIsSatisfied();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 2016-08-31
      • 2011-12-24
      • 2018-08-11
      相关资源
      最近更新 更多