如果我需要像下面那样模拟 RESTful 资源类和门面方法,门面将不会被模拟。
例如,
@Path("/v1/stocks")
public class StockResource {
@GET
@Path("/{stockid}")
@Produces({ MediaType.APPLICATION_JSON })
public Response getStock(@PathParam("stockid") String stockid) {
Stock stock = TestFacade.findStock(stockid);
if (!ObjectUtils.equals(stock, null)) {
return Response.status(Status.OK).entity(stock).build();
}
return Response.status(Status.BAD_REQUEST).build();
}
}
@RunWith(MockitoJUnitRunner.class)
public class StockTest{
RestClient restClient = new RestClient();
@Mock
private TestFacade facade;
@Test
public void getStockReturnsStock(){
// given
given(facade.findStock(stockid))
.willReturn(new Stock());
Resource resource = restClient.resource(url + "/1234");
// when
ClientResponse response = (ClientResponse) resource.accept(
"application/json").get();
// verify
assertEquals(200, response.getStatusCode());
verify(facade, Mockito.times(1)).findStock("stockid");
}
}
如何模拟 RESTful(JAX-RS) 资源类中的外观方法调用?有没有可能我可以在其中模拟资源类和方法调用。