如何对Jersey + Guice进行单元测试:ServiceLocator?

时间:2014-07-29 14:39:35

标签: java jersey guice jersey-2.0 guice-3

使用HK2 guice-bridge我设法将Jersey 2.x与Guice 3.x整合。

public class MyApp extends ResourceConfig {

    @Inject
    public MyApp(ServiceLocator serviceLocator) {
        packages("com.mycompany");

        ...

        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector(GuiceContext.INJECTOR);
    }
}

但现在我的泽西岛测试不再适用了。

public abstract class AbstractJerseyTest extends JerseyTest {

    public AbstractJerseyTest() throws TestContainerException {
        super(new InMemoryTestContainerFactory());
    }

    protected Application configure() {
        new MyApp(); // ERROR: missing 'ServiceLocator'
    }

}

那么我在哪里获得ServiceLocator单元测试?

4 个答案:

答案 0 :(得分:2)

也许更简洁的方法是简单地使用Feature并在那里配置而不是ResourceConfig

public class GuiceFeature implements Feature {

    public void configure(FeatureContext context) {
        ServiceLocator serviceLocator = ServiceLocatorProvider.getServiceLocator(context);
        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector(GuiceContext.INJECTOR);
    }
}

这可以像任何其他功能一样注册。只需register,或使用@Provider进行扫描。

请注意,ServiceLocatorProvider仅适用于Jersey 2.6及更高版本。

答案 1 :(得分:1)

这是一个有效的解决方案。

关键是覆盖JerseyTest的configureDeployment()方法并通过传递Application特定的ResourceConfig.class而不是重写configure()方法并返回ResourceConfig实例来创建DeploymentContext,以便测试容器正确初始化guice-bridge。

以下版本的Jersey,Guice和HK2 guice-bridge

    <jersey.version>2.15</jersey.version>
    <jackson2.version>2.4.4</jackson2.version>
    <hk2.guice.bridge.version>2.4.0-b10</hk2.guice.bridge.version>
    <guice.version>4.0-beta5</guice.version>

1)我的服务类

public interface MyService {
    public void hello();
}

2)我的模拟服务Impl

public class MyMockServiceImpl implements MyService{
    public void hello() {
        System.out.println("Hi");
    }
} 

3)我的资源类使用Guice注入服务

@Path("myapp")
public class MyResource {

    private final MyService myService;

    @Inject
    public MyResource(MyService myService) {
        this.myService = myService;
    }
}

4)我的资源测试课

public class MyResourceTest extends JerseyTestNg.ContainerPerClassTest {

    @Override
    protected Application configure() {
        return null;
    }

    @Override
    protected DeploymentContext configureDeployment() {
        return DeploymentContext.builder(MyTestConfig.class).build();
    }

    // other test and setup/teardown methods
}

5)ResourceConfig类

static class MyTestConfig extends ResourceConfig {

    @Inject
    public MyTestConfig(ServiceLocator serviceLocator) {

        packages("com.myapp.rest");

        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);

        GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector(Guice.createInjector(new MyTestModule()));

    }
}

6)我的Guice测试模块课程

public class MyTestModule implements Module {

    @Override
    public void configure(Binder binder) {
            binder.bind(MyService.class)
               .to(MyMockServiceImpl.class);
    }

}

答案 2 :(得分:0)

注意:我之前从未使用过泽西岛。但是,你不应该再打电话给new MyApp();否则Guice将无效。相反,我可能会尝试这样的事情:

public abstract class AbstractJerseyTest extends JerseyTest {
    private final Module[] modules;

    public AbstractJerseyTest(Module... modules) throws TestContainerException {
        super(new InMemoryTestContainerFactory());
        this.module = modules;
    }

    protected Application configure() {
        Injector inj = Guice.createInjector(modules);
        return inj.getInstance(MyApp.class);
    }
}

public class ActualTest extends AbstractJerseyTest {
    private static class TestModule extends AbstractModule {
        @Override
        public void configure() {
            // Do your guice bindings here
        }
    }

    public ActualTest() throws TestContainerException {
        super(new TestModule());
    }
}

答案 3 :(得分:0)

我们在Groovy的帮助下工作:

public class MemoryTestContainerFactory implements TestContainerFactory {

    private final Class<? extends Application> jaxrsApplicationClass;

    public MemoryTestContainerFactory(Class<? extends Application> jaxrsApplicationClass) {
        this.jaxrsApplicationClass = jaxrsApplicationClass;
    }

    @Override
    public TestContainer create(URI baseUri, DeploymentContext context) throws IllegalArgumentException {
        return new MemoryTestContainer(jaxrsApplicationClass, baseUri, context);
    }

    private static class MemoryTestContainer implements TestContainer {

        private final URI baseUri;
        private final ApplicationHandler appHandler;
        private final AtomicBoolean started = new AtomicBoolean(false);

        private static final Logger LOGGER = Logger.getLogger(MemoryTestContainer.class.getName());

        MemoryTestContainer(Class<? extends Application> jaxrsApplicationClass, URI baseUri, DeploymentContext context) {
            this.baseUri = UriBuilder.fromUri(baseUri).path(context.getContextPath()).build();    
            this.appHandler = new ApplicationHandler(jaxrsApplicationClass);
        }

        @Override
        public ClientConfig getClientConfig() {
            def provider = new InMemoryConnector.Provider(baseUri, appHandler) // private access (only works with Groovy)
            return new ClientConfig().connectorProvider(provider);
        }

        @Override
        public URI getBaseUri() {
            return baseUri;
        }

        @Override
        public void start() {
            if (started.compareAndSet(false, true)) {
                LOGGER.log(Level.FINE, "Starting InMemoryContainer...");
            } else {
                LOGGER.log(Level.WARNING, "Ignoring start request - InMemoryTestContainer is already started.");
            }
        }

        @Override
        public void stop() {
            if (started.compareAndSet(true, false)) {
                LOGGER.log(Level.FINE, "Stopping InMemoryContainer...");
            } else {
                LOGGER.log(Level.WARNING, "Ignoring stop request - InMemoryTestContainer is already stopped.");
            }
        }
    }
}

它不漂亮,但它有效。