LoginSignup
6
6

More than 5 years have passed since last update.

Jersey Test FrameworkとGuiceを一緒に使う

Posted at

こちらの記事でJerseyとGuiceを連携させる方法を紹介ましたが、今回はJersey Test FrameworkとGuiceを連携させる方法を調べました。ポイントはApplicationのコンストラクタにHK2のServiceLocatorをどうやって注入するかだけ。

HelloResource.java

package web.resources;

import web.MyApplication;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class HelloResourceTest extends JerseyTest {

    @Test
    public void testHello() throws Exception {
        String response = target("hello").request().get(String.class);
        assertThat(response, is("Hello!"));
    }

    @Override
    protected DeploymentContext configureDeployment() {
        return DeploymentContext.newInstance(MyApplication.class);
    }
}

JerseyTest.configureDeploymentをオーバーライドして、Class<? extends Application>を引数にとるDeploymentContextのファクトリメソッドを使うと、つつがなく注入されました。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6