Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JMock,获取错误_Java_Jmock - Fatal编程技术网

Java JMock,获取错误

Java JMock,获取错误,java,jmock,Java,Jmock,获取错误:java.lang.AssertionError:意外调用:user.setUserName(“John”) 未指定期望值:您是否。。。 -忘记用基数子句开始期望? -调用模拟方法以指定期望的参数? 在此之前发生了什么:什么都没有! 位于org.jmock.api.ExpectationError.unexpected(ExpectationError.java:23) 代码: Mockery context = new JUnit4Mockery(); @Test public v

获取错误: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");
    ;*/
}

设置期望值时,您可以通过对()的
调用返回的对象调用该方法来指定要模拟的方法,而不是对模拟本身(这不是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();
}