如何将依赖项注入 JerseyTest?
How to inject dependency into JerseyTest?
我想使用 CDI 将 MyService
直接注入我的 JerseyTest。可能吗? MyService
已成功注入 MyResource
,但当我尝试从 MyJerseyTest 访问它时出现 NullPointerException。
public class MyResourceTest extends JerseyTest {
@Inject
MyService myService;
private Weld weld;
@Override
protected Application configure() {
Properties props = System.getProperties();
props.setProperty("org.jboss.weld.se.archive.isolation", "false");
weld = new Weld();
weld.initialize();
return new ResourceConfig(MyResource.class);
}
@Override
public void tearDown() throws Exception {
weld.shutdown();
super.tearDown();
}
@Test
public void testGetPersonsCount() {
myService.doSomething(); // NullPointerException here
// ...
}
}
我认为您需要提供一个 org.junit.runner.Runner
的实例,您将在其中进行焊接初始化。该运行器还应负责提供 Test class 的实例,并注入必要的依赖项。示例如下
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
private final Class<?> clazz;
private final Weld weld;
private final WeldContainer container;
public WeldJUnit4Runner(final Class<Object> clazz) throws InitializationError {
super(clazz);
this.clazz = clazz;
// Do weld initialization here. You should remove your weld initialization code from your Test class.
this.weld = new Weld();
this.container = weld.initialize();
}
@Override
protected Object createTest() throws Exception {
return container.instance().select(clazz).get();
}
}
你的测试 class 应该用 @RunWith(WeldJUnit4Runner.class)
注释,如下所示。
@RunWith(WeldJUnit4Runner.class)
public class MyResourceTest extends JerseyTest {
@Inject
MyService myService;
// Test Methods follow
}
我想使用 CDI 将 MyService
直接注入我的 JerseyTest。可能吗? MyService
已成功注入 MyResource
,但当我尝试从 MyJerseyTest 访问它时出现 NullPointerException。
public class MyResourceTest extends JerseyTest {
@Inject
MyService myService;
private Weld weld;
@Override
protected Application configure() {
Properties props = System.getProperties();
props.setProperty("org.jboss.weld.se.archive.isolation", "false");
weld = new Weld();
weld.initialize();
return new ResourceConfig(MyResource.class);
}
@Override
public void tearDown() throws Exception {
weld.shutdown();
super.tearDown();
}
@Test
public void testGetPersonsCount() {
myService.doSomething(); // NullPointerException here
// ...
}
}
我认为您需要提供一个 org.junit.runner.Runner
的实例,您将在其中进行焊接初始化。该运行器还应负责提供 Test class 的实例,并注入必要的依赖项。示例如下
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
private final Class<?> clazz;
private final Weld weld;
private final WeldContainer container;
public WeldJUnit4Runner(final Class<Object> clazz) throws InitializationError {
super(clazz);
this.clazz = clazz;
// Do weld initialization here. You should remove your weld initialization code from your Test class.
this.weld = new Weld();
this.container = weld.initialize();
}
@Override
protected Object createTest() throws Exception {
return container.instance().select(clazz).get();
}
}
你的测试 class 应该用 @RunWith(WeldJUnit4Runner.class)
注释,如下所示。
@RunWith(WeldJUnit4Runner.class)
public class MyResourceTest extends JerseyTest {
@Inject
MyService myService;
// Test Methods follow
}