访问JerseyTest中的Spring bean

eba*_*axt 7 testing spring integration-testing unit-testing jersey

我试图弄清楚如何从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());
    }

}
Run Code Online (Sandbox Code Playgroud)

该设置使用默认的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);
    }
Run Code Online (Sandbox Code Playgroud)

有人能指出我正确的方向吗?

Lui*_*osa 10

我正在使用一种天真的方法,但有效

public ResourceIT()
    {
        super(new WebAppDescriptor.Builder("amazingpackage")
                .servletClass(SpringServlet.class)
                .contextParam("contextConfigLocation", "classpath:/spring/context.xml")
                .contextListenerClass(ContextLoaderListener.class)
                .contextPath("context")
                .build());
        injectedBean = ContextLoaderListener
                .getCurrentWebApplicationContext().getBean(InjectedBean.class);
    }
Run Code Online (Sandbox Code Playgroud)


eba*_*axt 2

使用此处描述的解决方案已经一周了,效果很好。