è¿éä»ç»å¦ä¸ä¸ªæ¹æ³ï¼å©ç¨âå
åä¸ç容å¨âæ¥è°è¯ï¼å°±æ¯æ们ä¸ç¨æå
并æå°tomcatä¸ï¼èªå·±å¨IDEä¸ï¼ç¨Unit Testçæ¹æ³æ¥æµè¯ã
1. Jerseyçæµè¯æ¡æ¶æ¯æç容å¨å¾å¤ï¼è¿ééç¨äºå¸¸ç¨çgrizzly2
å¨é¡¹ç®çpom.xmlä¸ï¼å¼å
¥ä¾èµï¼
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
2. åJerseyçResourceæ件
@Path("my/jersey")
public class TestResource{
@Autowired
protected SystemManager systemManager; //è¿ç±spring注å
¥
@GET
@Path("/test")
public String test(@QueryParam("systemId") Integer systemId) {
return "test";
}
}
3. 书åSystemManageråSystemManagerImpl
ç¥
4. æmanageré
ç½®å°Spring Application.xmlä¸
ç¥
5. 书åJersey Application
@Path("webapi")
public class TestApplication extends ResourceConfig {
public TestApplication(){
register(RequestContextFilter.class);
register(TestResource.class);
}
}
6. 书åUnit Testæ件
public class MyRestTest extends JerseyTest {
@Override
protected Application configure() {
ResourceConfig rc = new MyApplication()
.register(SpringLifecycleListener.class)
.register(RequestContextFilter.class);
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
return configure(rc);
}
@Override
protected ResourceConfig configure(ResourceConfig rc) {
rc.property("contextConfigLocation", "spring.xml");
return rc;
}
@Test
public void test(){
final String hello = target("my/jersey/test")
.queryParam("systemId", 1)
.request()
.get(String.class);
System.out.println("==========\n" + hello);
}
}
7. è¿è¡test(ï¼ï¼å³å¯çå°ç»æã
温馨提示:答案为网友推荐,仅供参考