获取错误: 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");
;*/
}发布于 2013-11-07 15:23:07
在设置期望时,您可以通过在调用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();
}https://stackoverflow.com/questions/19266110
复制相似问题