我想用以下方法测试我的REST服务:
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>1.19</version>
<scope>test</scope>
</dependency>
我有配置类:
public class MyServiceTest extends JerseyTest {
@Override
protected int getPort(int defaultPort) {
return 8080;
}
public static class AppConfig extends DefaultResourceConfig {
public AppConfig() {
super(MyService.class);
}
}
@Override
public WebAppDescriptor configure() {
return new WebAppDescriptor.Builder()
.initParam(WebComponent.RESOURCE_CONFIG_CLASS,
AppConfig.class.getName())
.build();
}
public MyServiceTest(){
}
}
MyService.java是REST端点,它注入了DAO和其他服务。在MyService.java中有一些setter用于模拟目的。如何使用set / mocked相关类提供MyService实例?
答案 0 :(得分:2)
它适用于
public static class AppConfig extends DefaultResourceConfig {
public AppConfig() {
MyService myService = new MyService();
MyDAO myDAO = mock(MyDAO.class);
myService.setMyDAO(myDAO);
getSingletons().add(new ExceptionMapperProvider());
getSingletons().add(myService);
}
}