我通过实施定制InjectableProvider找到了解决方案。以下代码取自Oracle article:
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.ext.Provider;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;
@Provider
public class EJBProvider implements InjectableProvider<EJB, Type> {
public Scope getScope() {
return Scope.Singleton;
}
public Injectable getInjectable(ComponentContext cc, EJB ejb, Type t) {
if (!(t instanceof Class)) return null;
try {
Class c = (Class)t;
Context ic = new InitialContext();
final Object o = ic.lookup(c.getName());
return new Injectable<Object>() {
public Object getValue(HttpContext c) {
return o;
}
};
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
我不得不稍微调整它以适合我的环境。还要注意提供者必须和你的服务类在同一个包中,否则它将不会被拾取(它在文章中没有说)。
模拟'Mapper'。除非您需要完整的集成测试,否则您需要可以加载完整EE环境的东西。也许像arquillian。 –
你可以通过Mockito嘲笑'Mapper'看到一种方式[这里](http://stackoverflow.com/a/27447345/2587435) –
我在考虑集成测试,因为单元测试已经完成;所以我想使用轻型EE嵌入式容器。 –