我已经实施了一些Restful服务,并使用Google App Engine作为数据库部分。 一切正常,我用cURL测试过。问题是我需要创建 一组针对类似服务的单元测试。
这是我得到的错误:
Jan 23, 2012 10:10:42 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Jan 23, 2012 10:10:42 AM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer <init>
INFO: Creating low level InMemory test container configured at the base URI http://localhost:9998/
Jan 23, 2012 10:10:42 AM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer start
INFO: Starting low level InMemory test container
Jan 23, 2012 10:10:42 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.11 12/09/2011 10:27 AM'
Jan 23, 2012 10:10:43 AM com.sun.jersey.spi.inject.Errors processErrorMessages
WARNING: The following warnings have been detected with resource and/or provider classes:
WARNING: A HTTP GET method, public void org.mycompany.myservices.Cache.flush(java.lang.String), MUST return a non-void type.
Jan 23, 2012 10:10:43 AM com.sun.jersey.test.framework.spi.container.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer stop
INFO: Stopping low level InMemory test container
我的WS实现:
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Building put(@HeaderParam("Authorization") String token, Building building) {
try {
BuildingsDelegator td = new BuildingsDelegator(verifyCredentials(token));
return td.createBuilding(building);
} catch (AuthenticationException e) {
building = new Building();
building.setError("authentication did not succeed");
return building;
}
}
我遇到的测试用例:
public class BuildingTest extends JerseyTest {
private String token ="DQAAAMMAAAB9ApLCubrBvuac0Y6lF8TH8FPXY5WzAhkwrqZv6tL0l9pu_xcY_9mVa1Q-sqTDlMXWUGoo8coYZGh8H2L2b_597O6StOyZ7N3uD-6v5HLNgPDWjj6YXl_X_LyEGM97z3JbSFire3J2Aw5Rag09BEweSMEjV-7TX3lqvtipWYJwQNWlSKpDl4F3TxBj-bWQulvFQ_wonLKze227Le3D_gB0JRofe3L5RkxBClkR6VKIx5s9fEbzupFz6t9hmKoNUTdRa14CgrQSaxXiPMO7J6G9";
@Override
protected AppDescriptor configure() {
this.setTestContainerFactory(new InMemoryTestContainerFactory() );
return new WebAppDescriptor.Builder("org.mycompany.myservices")
.contextPath("/").build(); }
@Test
public void put() {
Building building = new Building();
building.setBuildingId("test_id_1.0x");
building.setTeamId("weakId");
building.setBuildingStatus(2);
building.setRunId((long) 1110705);
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:9998/");
RunBean resultBean = webResource
.path("rest/building")
.header("Authorization", token)
.type(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE)
.post(RunBean.class,building);
assertEquals(((Building)resultBean).getBuildingId(), building.getBuildingId());
}
}