0 junit unit-testing jax-rs resteasy
我正在使用Resteasy服务器端模拟框架来测试我的服务.我不想测试业务逻辑,但我想测试服务生成的数据.
使用这种方法,我可以创建一个简单的测试.但是,在我的RestEasy服务中,我有一些依赖,我想嘲笑.
请参阅以下我要测试的示例服务.必须嘲笑协作者,以便测试服务.
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
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(..);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在测试中嘲笑合作者?
小智 5
这对我使用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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9416 次 |
| 最近记录: |