简体   繁体   English

使用Jersey Test,Grizzly和HK2依赖注射进行功能测试

[英]Functional tests with Jersey Test, Grizzly and HK2 Dependency Injection

I'm attempting to write functional tests for my REST API using the Jersey Test framework. 我正在尝试使用Jersey Test框架为我的REST API编写功能测试。 However, I've seem to hit a roadblock when it comes to using dependency injection within my functional tests. 但是,在我的功能测试中使用依赖注入时,我似乎遇到了障碍。 My main application looks like this: 我的主要应用程序如下所示:

@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();
    }
}

Notice here that I'm using the HK2's ServiceLocatorUtilities.createAndPopulateServiceLocator() method in order to read the hk2-metadata-generator file. 请注意,我正在使用HK2的ServiceLocatorUtilities.createAndPopulateServiceLocator()方法来读取hk2-metadata-generator文件。 This method creates a ServiceLocator object which then in turn is passed to the GrizzlyHttpServerFactory.createHttpServer method. 此方法创建一个ServiceLocator对象,然后将该对象传递给GrizzlyHttpServerFactory.createHttpServer方法。 This all works great for running the Grizzly server, however, the question I have now is how do I create functional tests for my application with the Jersey Test Framework? 这对于运行Grizzly服务器非常有用,但是,我现在的问题是如何使用Jersey Test Framework为我的应用程序创建功能测试?

My unit test currently looks like this: 我的单元测试目前看起来像这样:

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());
    }

}

Is there even a way to use the HK2 service locator with the Jersey Test framework or do I need to treat my application as an external container and use the external container provider as documented here: External container ? 有没有办法将HK2服​​务定位器与Jersey Test框架一起使用,或者我是否需要将我的应用程序视为外部容器并使用外部容器提供程序,如下所示: 外部容器

Also, since these are functional tests, mocking the injected services is not an option here. 此外,由于这些是功能测试,因此模拟注入的服务不是一种选择。

You can use the Locator Bridge to take two separate locator (the one you created and the one from Jersey) and bridge them together. 您可以使用定位器桥来获取两个单独的定位器(您创建的定位器和来自Jersey的定位器)并将它们连接在一起。 The bridge can be made bi-directional as well (within limits) and so it'll appear in most normal usage to be one large ServiceLocator. 桥也可以是双向的(在限制范围内),因此它在大多数正常使用中都会出现一个大的ServiceLocator。

Note that there was a bug fixed this week with the ServiceLocator bridge which has not yet been pushed out to maven but will (probably) be pushed sometime next week. 请注意,本周修复了ServiceLocator桥的一个错误,该桥尚未被推送到maven但是(可能)将在下周的某个时间推送。 See HK2-295 HK2-295

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM