如何使用 Jersey 测试框架启用 CDI?
How can I enable CDI with Jersey Test Framework?
我找到了 How can I inject a data source dependency into a RESTful web service with Jersey (Test Framework)?,但我想我会问一些不同的问题。
这是
的后续问题
我写了一个 JAX-RS 库,我正在尝试使用 Jersey Test Framework 进行单元测试。
我好像HK2注入正常。但是我发现我的一些用 @PostConstruct
或 @PreDestroy
注释的生命周期拦截器方法没有被调用(或者只有一些被调用)。
public class MyResource {
@PostConstruct
private void constructed() { // not invoked
}
@Inject
private Some some; // injection works.
}
如何使用 Jersey 测试框架启用 CDI?我要依赖什么样的神器?
这是我当前的依赖项。
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
我找到了解决办法。
我添加了以下附加依赖项。
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<scope>test</scope>
</dependency>
现在 Weld 接管了 HK2,我想。我不知道 jersey-cdi1x-ban-custom-hk2-binding
是干什么用的。无论如何,我可以使用 javax.enterprise:cdi-api
.
中的标准注释
public class MyProducer {
@Produces @Some
public MyType produceSome() {}
public void disposeSome(@Disposes @Some MyType instance) {}
}
并添加了 Weld 的初始化代码。
@Override
protected Application configure() {
// this method works somewhat weirdly.
// local variables including logger
// is null in here
// I have to start (and join) a thread
// which initializes Weld and adds a shutdown hook
final Thread thread = new Thread(() -> {
final Weld weld = new Weld();
weld.initialize();
Runtime.getRuntime().addShutdownHook(
new Thread(() -> weld.shutdown()));
});
thread.start();
try {
thread.join();
} catch (final InterruptedException ie) {
throw new RuntimeException(ie);
}
final ResourceConfig resourceConfig
= new ResourceConfig(MyResource.class);
resourceConfig.register(MyProducer.class);
return resourceConfig;
}
注入每个点并调用所有生命周期方法。耶!!!
我不明白为什么我首先尝试使用线程。
@Override
protected Application configure() {
final Weld weld = new Weld();
weld.initialize();
Runtime.getRuntime().addShutdownHook(new Thread(() -> weld.shutdown()));
final ResourceConfig resourceConfig
= new ResourceConfig(MyResource.class);
resourceConfig.register(MyProducer.class);
return resourceConfig;
}
因为我使用 JerseyTestNg.ContainerPerClassTest
我失败了,至少在 TestNG 中,无法使用 @BeforeClass
和 @AfterClass
因为 configure()
方法是从构造函数(间接)调用的.
我想我可以使用 @BeforeMethod
和 @AfterMethod
进行 initializing/shutting-down 焊接,如果我切换到 JerseyTestNg.ContainerPerMethodTest
。
jersey-cdi1x
是 jersey-weld2-se
的传递依赖,因此可以省略。
我找到了 How can I inject a data source dependency into a RESTful web service with Jersey (Test Framework)?,但我想我会问一些不同的问题。
这是
我写了一个 JAX-RS 库,我正在尝试使用 Jersey Test Framework 进行单元测试。
我好像HK2注入正常。但是我发现我的一些用 @PostConstruct
或 @PreDestroy
注释的生命周期拦截器方法没有被调用(或者只有一些被调用)。
public class MyResource {
@PostConstruct
private void constructed() { // not invoked
}
@Inject
private Some some; // injection works.
}
如何使用 Jersey 测试框架启用 CDI?我要依赖什么样的神器?
这是我当前的依赖项。
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
我找到了解决办法。
我添加了以下附加依赖项。
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<scope>test</scope>
</dependency>
现在 Weld 接管了 HK2,我想。我不知道 jersey-cdi1x-ban-custom-hk2-binding
是干什么用的。无论如何,我可以使用 javax.enterprise:cdi-api
.
public class MyProducer {
@Produces @Some
public MyType produceSome() {}
public void disposeSome(@Disposes @Some MyType instance) {}
}
并添加了 Weld 的初始化代码。
@Override
protected Application configure() {
// this method works somewhat weirdly.
// local variables including logger
// is null in here
// I have to start (and join) a thread
// which initializes Weld and adds a shutdown hook
final Thread thread = new Thread(() -> {
final Weld weld = new Weld();
weld.initialize();
Runtime.getRuntime().addShutdownHook(
new Thread(() -> weld.shutdown()));
});
thread.start();
try {
thread.join();
} catch (final InterruptedException ie) {
throw new RuntimeException(ie);
}
final ResourceConfig resourceConfig
= new ResourceConfig(MyResource.class);
resourceConfig.register(MyProducer.class);
return resourceConfig;
}
注入每个点并调用所有生命周期方法。耶!!!
我不明白为什么我首先尝试使用线程。
@Override
protected Application configure() {
final Weld weld = new Weld();
weld.initialize();
Runtime.getRuntime().addShutdownHook(new Thread(() -> weld.shutdown()));
final ResourceConfig resourceConfig
= new ResourceConfig(MyResource.class);
resourceConfig.register(MyProducer.class);
return resourceConfig;
}
因为我使用 JerseyTestNg.ContainerPerClassTest
我失败了,至少在 TestNG 中,无法使用 @BeforeClass
和 @AfterClass
因为 configure()
方法是从构造函数(间接)调用的.
我想我可以使用 @BeforeMethod
和 @AfterMethod
进行 initializing/shutting-down 焊接,如果我切换到 JerseyTestNg.ContainerPerMethodTest
。
jersey-cdi1x
是 jersey-weld2-se
的传递依赖,因此可以省略。