質問

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

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.");
            }
        }
    }
}
.

それはきれいではありませんが機能します。

他のヒント

これは機能した解決策です。

keyは、configure()メソッドをオーバーライドする代わりにアプリケーション固有のResourconfig.classを渡して、jerseytestのprysioneployment()メソッドをオーバーライドし、ResourceConfigインスタンスを返すことでDeploymentContextを作成することです。テストコンテナはGuice-Bridgeを正しく初期化します。

これはジャージー、ガス、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)私のモックサービスは

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

3)ガス注入サービス

@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)マイガステストモジュールクラス

public class MyTestModule implements Module {

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

}
.

注:私は前にジャージを使いました。ただし、new MyApp()を呼び出すべきではありません。さもなければガスはうまくいきません。代わりに、私はこのようなものを試すかもしれません:

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top