在 JerseyTest 中访问 Spring beans
我试图弄清楚如何从 JerseyTest 的子类访问 Spring bean。 扩展 JerseyTest 我已经设法在测试中加载 Spring 上下文,但我还没有弄清楚如何访问 spring 上下文。我的设置如下所示:
public abstract class SpringJerseyTest extends JerseyTest {
public SpringJerseyTest() throws Exception {
super(new WebAppDescriptor.Builder("com.acme.resources")
.contextPath("/")
.contextParam("contextConfigLocation", "classpath:applicationContext.xml")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.build());
}
}
该设置使用默认的 Grizzly Web 容器。我以前从未使用过 Grizzly,但在 Jetty 中我会做这样的事情:
public Object getSpringBean(String beanName) {
WebAppContext context = (WebAppContext) server.getHandler();
ServletContext sc = context.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
return applicationContext.getBean(beanName);
}
有人能指出我正确的方向吗?
I'm trying to figure out how to access Spring beans from a subclass of JerseyTest.
Extending JerseyTest I've managed to load the Spring context in my tests, but I haven't figured out how to access the spring context. My setup looks like this:
public abstract class SpringJerseyTest extends JerseyTest {
public SpringJerseyTest() throws Exception {
super(new WebAppDescriptor.Builder("com.acme.resources")
.contextPath("/")
.contextParam("contextConfigLocation", "classpath:applicationContext.xml")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.build());
}
}
The setup is using the default Grizzly Web Container. I've never used Grizzly before, but in Jetty I would do something like this:
public Object getSpringBean(String beanName) {
WebAppContext context = (WebAppContext) server.getHandler();
ServletContext sc = context.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
return applicationContext.getBean(beanName);
}
Could anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用的是一种幼稚的方法,但是有效
I am using a naive approach but works
一直在使用此处描述的解决方案已经用了一周了,效果还不错。
Been using the solution described here for a week, and it's working fine.
我不明白使用 Spring bean 的 JerseyTest 的需求,通常你的 Spring Bean 是 Service/Dao 层,它们必须在其层上进行单元测试/集成测试,使用 Mockito 或 DBUnit(集成测试)。
我一直使用 Sprig bean 作为模拟对 Jersey 资源类进行单元测试,这是因为您必须隔离测试,并在 JerseyTest 中仅测试 Jersey 内容(和 Json),而不是 Service 或 Dao 层。,是的,我确实通过了我的 spring bean上下文,但 spring bean 只是模拟,因为我不想在 JerseyTests 中测试 spring bean。
如果您隔离测试,那么编写和维护测试会更容易
I dont understand the need of JerseyTest that uses Spring bean, often your Spring Beans are Service/Dao layer and they have to be Unit test / Integration Test on their layer, using Mockito or DBUnit (integration Tests).
I have been unit Testing Jersey Resource classes using Sprig beans as mocks, this is because you have to isolate the tests, and test only Jersey stuff (and Json) in JerseyTest, not Service or Dao Layer., Yes I do pass my spring bean context, but the spring beans are only mocks, cause I don't want to test the spring beans in JerseyTests.
If you isolate you tests it would be easier to write and maintain your tests