Java Jersey测试、Grizzly和HK2依赖注入的功能测试
我正试图使用Jersey测试框架为我的RESTAPI编写功能测试。然而,在功能测试中使用依赖注入时,我似乎遇到了一个障碍。我的主要应用程序如下所示:Java Jersey测试、Grizzly和HK2依赖注入的功能测试,java,unit-testing,jersey-2.0,hk2,jersey-test-framework,Java,Unit Testing,Jersey 2.0,Hk2,Jersey Test Framework,我正试图使用Jersey测试框架为我的RESTAPI编写功能测试。然而,在功能测试中使用依赖注入时,我似乎遇到了一个障碍。我的主要应用程序如下所示: @ApplicationPath("/") public class Application extends ResourceConfig { private static final URI BASE_URI = URI.create("http://localhost:8080/api/"); public static v
@ApplicationPath("/")
public class Application extends ResourceConfig {
private static final URI BASE_URI = URI.create("http://localhost:8080/api/");
public static void main(String[] args) throws Exception {
System.out.println("Starting application...");
final ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
final ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(JacksonFeature.class);
resourceConfig.register(LoggingFeature.class);
resourceConfig.packages(true, "my.package.name");
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, locator);
Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow));
server.start();
Thread.currentThread().join();
}
}
public class FormsResourceTest extends JerseyTest {
@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new GrizzlyWebTestContainerFactory();
}
@Test
public void testMe() {
Response response = target("/test").request().get();
assertEquals("Should return status 200", 200, response.getStatus());
}
}
请注意,我正在使用HK2的ServiceLocatorUtilities.createAndPopulateServiceLocator()
方法来读取HK2元数据生成器
文件。此方法创建一个ServiceLocator
对象,然后将该对象传递给GrizzlyHttpServerFactory.createHttpServer
方法。这一切对于运行Grizzly服务器非常有用,但是,我现在的问题是如何使用Jersey测试框架为我的应用程序创建功能测试
我的单元测试目前如下所示:
@ApplicationPath("/")
public class Application extends ResourceConfig {
private static final URI BASE_URI = URI.create("http://localhost:8080/api/");
public static void main(String[] args) throws Exception {
System.out.println("Starting application...");
final ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
final ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(JacksonFeature.class);
resourceConfig.register(LoggingFeature.class);
resourceConfig.packages(true, "my.package.name");
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, locator);
Runtime.getRuntime().addShutdownHook(new Thread(server::shutdownNow));
server.start();
Thread.currentThread().join();
}
}
public class FormsResourceTest extends JerseyTest {
@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new GrizzlyWebTestContainerFactory();
}
@Test
public void testMe() {
Response response = target("/test").request().get();
assertEquals("Should return status 200", 200, response.getStatus());
}
}
是否有办法将HK2服务定位器与Jersey测试框架一起使用,或者我是否需要将我的应用程序视为一个外部容器,并使用此处记录的外部容器提供程序:
此外,由于这些都是功能测试,模拟注入的服务在这里不是一个选项。您可以使用获取两个单独的定位器(您创建的定位器和来自Jersey的定位器)并将它们连接在一起。桥接器也可以是双向的(在限制范围内),因此在大多数正常使用中,它会显示为一个大型ServiceLocator
请注意,ServiceLocator桥在本周修复了一个bug,该bug尚未发布到maven,但(可能)将在下周某个时候发布。请参见,您可以使用获取两个单独的定位器(您创建的定位器和来自Jersey的定位器)并将它们连接在一起。桥接器也可以是双向的(在限制范围内),因此在大多数正常使用中,它会显示为一个大型ServiceLocator
请注意,ServiceLocator桥在本周修复了一个bug,该bug尚未发布到maven,但(可能)将在下周某个时候发布。请参见为什么不能使用AbstractBinder绑定依赖项(接口到impl),然后将绑定器注册到ResourceConfig并将ResourceConfig传递到服务器?@Raf这实际上是我最终的方向。我发现最好是显式声明我的依赖项,而不是扫描包来查找它们。为什么不能使用AbstractBinder绑定依赖项(接口到impl),然后将绑定项注册到ResourceConfig并将ResourceConfig传递到服务器?@Raf这实际上是我最终的方向。我发现最好是显式地声明我的依赖项,而不是扫描包来查找它们。