我想测试下面的类实现带有CDI Injects的JAX-RS。
@Path("area")
public class AreaService {
@Inject
@SuppressWarnings("unused")
private Logger logger;
@Context
private SecurityContext context;
@Inject
private AreaGateway areaGateway;
@GET
@Path("{area-name}/team-project/{tp-id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getAreaByTeamProjectAndName(@PathParam("tp-id") long tpId, @PathParam("area-name") String areaName)
throws WebApplicationException {
Area area = areaGateway.getAreaByTeamProjectAndName(tpId, areaName);
return Response.status(Response.Status.OK).entity(area).build();
}
@POST
@Path("team-project/{tp-id}")
@Auditable
@Consumes(MediaType.APPLICATION_JSON)
public Response createArea(@PathParam("tp-id") long tpId, @Payload Area area) throws WebApplicationException {
long areaId = areaGateway.createArea(tpId, area, context);
return Response.status(Response.Status.CREATED).entity(areaId).build();
}
}
我选择了Arquillian作为测试框架,我使用了the extension作为REST API。
特别是我试图遵循Warp REST示例,但是我找不到一个干净清晰的示例来测试我的REST API,使用任何模拟框架(如Mockito)模拟注入依赖性,如AreaGateway。
我想做这样的事情:
@RunWith(Arquillian.class)
public class RestClientTestCase {
@ArquillianResource
public URL contextPath;
private AreaService areaService;
private AreaGateway areaGateway;
@Deployment(testable = false)
public static WebArchive create() {
WebArchive war = ShrinkWrap.create(WebArchive.class)
.addClasses(SLF4JProducer.class, AreaGateway.class, JAXRSActivator.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml");
MavenResolverSystem resolver = Maven.resolver();
war.addAsLibraries(resolver.loadPomFromFile("pom.xml").resolve("org.mockito:mockito-all:1.10.8").withTransitivity().asSingleFile());
return war;
}
@BeforeClass
public static void setUpClass() {
// initializes the rest easy client framework
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
}
@Before
public void setUp() {
areaGateway = Mockito.mock(AreaGateway.class);//real bean replaced by mocked one
Mockito.when(areaGateway.getAreaByTeamProjectAndName(0, "areaName")).thenReturn(new Area());
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("" + "rest");
areaService = target.proxy(AreaService.class);
}
@Test
@RunAsClient
public void testStockGetWarp() {
Response response = areaService.getAreaByTeamProject(0);
assertEquals("The service returned status code.", 201, response.getStatus());
response.close();
}
}
有人可以帮我开发具有精确Maven依赖关系的最小测试样本吗?根据Arquillian,是否有可能用某种框架模拟CDI依赖?