如何单元测试Jersey+Guice:服务定位器?
-
21-12-2019 - |
题
使用HK2的 guice-bridge
我设法整合了Jersey2。X与Guice3。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.");
}
}
}
}
.
它不漂亮,但它有效。
其他提示
也许更清洁的方法是简单地使用 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
只有球衣2.6及以上。
这是一个解决的解决方案。
密钥是覆盖jerseytest的DefableDe雇工()方法,并通过传递应用程序特定的ResourceConfig.Class而不是覆盖Configure()方法并返回ResourceConfig实例来创建DeploymentContext,以便正确地初始化Guice-Bridge。
这是通过以下版本的泽西,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)我的模拟服务ich
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);
}
}
. 注:我以前从未使用过泽西岛。但是,你不应该打电话 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());
}
}
不隶属于 StackOverflow