Resteasy服务器端模拟框架 [英] Resteasy Server-side Mock Framework
本文介绍了Resteasy服务器端模拟框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Resteasy服务器端模拟框架来测试我的服务.我不想测试业务逻辑,但是我想测试服务产生的数据.
I am using Resteasy serverside mock framework to test my service. I dont want to test business logic but I would like test the data produced by the service.
使用此方法我能够创建一个简单的测试.但是,在我的RestEasy服务中,我要模拟一些依赖项.
Using this approach I am able to create a simple test. However, in my RestEasy service I have a few dependency which I would like to mock.
请参阅以下我要测试的示例服务.必须模拟协作者,以便可以测试服务.
See the following example service which I would like test. The collaborator must be mocked so the service can be tested.
@Path("v1")
Class ExampleService {
@inject
private Collaborator collaborator;
@GET
@Path("/")
@Produces({ "application/xml", "application/json" })
public Response getDummy() throws WSAccessException, JsonParseException, JsonMappingException, IOException {
...
Result result = collaborator.getResult();
..
return Response.ok("helloworld").build();
}
}
junit测试如下
@Test
public void testfetchHistory() throws URISyntaxException {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
POJOResourceFactory noDefaults = new POJOResourceFactory(ExampleService.class);
dispatcher.getRegistry().addResourceFactory(noDefaults);
MockHttpRequest request = MockHttpRequest.get("v1/");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
Assert.assertEquals(..);
}
如何在测试中嘲笑协作者?
How can I mock the collaborator in the test?
推荐答案
使用EasyMock对我有用
This worked for me using EasyMock
@Test
public void testfetchHistory() throws URISyntaxException {
Collaborator mockCollaborator = EasyMock.createMock(Collaborator.class);
Result result = new Result();
EasyMock.expect(mockCollaborator.getResult()).andReturn(result);
EasyMock.replay(mockCollaborator);
ExampleService obj = new ExampleService();
obj.setCollaborator(mockCollaborator);
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(obj);
MockHttpRequest request = MockHttpRequest.get("v1/");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
Assert.assertEquals(..);
EasyMock.verify(mockCollaborator);
}
这篇关于Resteasy服务器端模拟框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文