Java 如何使用 EasyMock 模拟方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24302965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I mock a method using EasyMock
提问by robasta
User getUser(int id) {
User user = service.get( id );
if( user != null ) {
user.Stuff = getUserStuff( id );
return User;
}
throw new NotFoundException();
}
Stuff getUserStuff( int id ) {
stuffGetter.getStuff( id ); // stuff getter makes a rest call
return Stuff;
}
Using EasyMock, how would i test the getUser(id)
method. The confusing part for me is that getUserStuff(id)
makes an external call which I dont want to make when testing getUser
.
使用 EasyMock,我将如何测试该getUser(id)
方法。对我来说,令人困惑的部分是getUserStuff(id)
进行了一个我不想在测试时进行的外部调用getUser
。
采纳答案by Dan Temple
As mentioned by other users in the comments, you need to provide mock instances of the objects that are making the calls that you wish to mock out.
正如其他用户在评论中提到的,您需要提供进行您希望模拟的调用的对象的模拟实例。
So within getUser(id)
I can see a call to an object called service
which should be mocked out. So use a mock instance of the service class and then the call to get(id)
can be mocked out.
所以在getUser(id)
我可以看到对一个被调用的对象的调用service
,它应该被模拟出来。所以使用服务类的模拟实例,然后get(id)
可以模拟出调用。
Then within the getUserstuff(id)
method, it uses a stuffGetter
object which could be mocked out again. Then the method being called could have expectations set up for it.
(An alternative to this could also be to set up a partial mock and mock the getUSerstuff(id)
method all together, but this is a bit beyond what you're looking to do I think)
然后在该getUserstuff(id)
方法中,它使用一个stuffGetter
可以再次模拟的对象。然后被调用的方法可以为其设置期望。(对此的替代方法也可以是设置一个部分模拟并getUSerstuff(id)
一起模拟该方法,但这有点超出了我认为您想要做的事情)
With those things in mind, I created the following class to test your described use case:
考虑到这些,我创建了以下类来测试您描述的用例:
public class ClassToTest {
private final Service service;
private final StuffGetter stuffGetter;
public ClassToTest() {
this( new Service(), new StuffGetter() );
}
public ClassToTest(final Service service, final StuffGetter stuffGetter) {
this.service = service;
this.stuffGetter = stuffGetter;
}
public User getUser(final int id) {
final User user = this.service.get( id );
if( user != null ) {
user.stuff = getUserStuff( id );
return user;
}
throw new NotFoundException();
}
public Stuff getUserStuff( final int id ) {
return this.stuffGetter.getStuff( id );
}
}
Having the constructors set up like this gives me the ability to set up mock instances of the dependencies within the tests. So what follows is a test for the getUser(id)
method that uses mocks for the two objects. It uses the basic EasyMock concepts of expect, replay and verify.
像这样设置构造函数使我能够在测试中设置依赖项的模拟实例。那么接下来就是getUser(id)
对这两个对象使用模拟的方法的测试。它使用了基本的 EasyMock 概念,即expect、replay 和verify。
- Create mock instances for the objects you need to mock the method calls on, in this case the
service
and thestuffGetter
. - Write expectations for the method calls using the
expect
method. This is where you specify what will be returned if the method you are mocking is not a void method. - Replay the mock objects, in this case replay both of them. This means you can no longer add any expectations because the mocks are no longer in record mode for recording expectations.
- Execute your tests.
- Verify that the expected method calls on your mocks were executed as desired.
- 为您需要模拟方法调用的对象创建模拟实例,在本例中为
service
和stuffGetter
。 - 使用
expect
方法编写对方法调用的期望。如果您模拟的方法不是 void 方法,您可以在此处指定将返回的内容。 - 重放模拟对象,在这种情况下重放它们。这意味着您不能再添加任何期望,因为模拟不再处于记录模式以记录期望。
- 执行您的测试。
- 验证模拟上的预期方法调用是否按需要执行。
The tests looks like this:
测试如下所示:
import static org.junit.Assert.assertEquals;
import org.easymock.EasyMock;
import org.junit.Test;
public class ClassToTestTest {
@Test
public void thatGetIdCallsExpectedMockMethods() {
final User user = new User();
final Stuff userStuff = new Stuff();
final Service mockService = EasyMock.createMock(Service.class);
final StuffGetter mockStuffGetter = EasyMock.createMock(StuffGetter.class);
EasyMock.expect( mockService.get(15) ).andReturn( user );
EasyMock.expect( mockStuffGetter.getStuff(15) ).andReturn( userStuff );
EasyMock.replay( mockService, mockStuffGetter );
final ClassToTest classToTest = new ClassToTest( mockService, mockStuffGetter );
final User returnedUser = classToTest.getUser(15);
assertEquals(returnedUser, user);
assertEquals(returnedUser.stuff, userStuff);
EasyMock.verify( mockService, mockStuffGetter );
}
}