JerseyTestのサブクラスからSpring Beanにアクセスする方法を理解しようとしています。 JerseyTestの拡張私はテストでSpringコンテキストをロードすることができましたが、Springコンテキストにアクセスする方法を理解していません。私の設定は次のようになります:JerseyTestでSpring Beanにアクセスする
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 Containerを使用しています。私は前にグリズリーを使ったことがないが、突堤に、私はこのようなものだろう:
public Object getSpringBean(String beanName) {
WebAppContext context = (WebAppContext) server.getHandler();
ServletContext sc = context.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
return applicationContext.getBean(beanName);
}
誰もが正しい方向に私を指すでしたか?
わからない、あなたのテストを維持することが容易になるだろうが、私は、このブログ投稿を見つけた:http://geek.riffpie.com/2010/ 08/09/unit-testing-restful-jersey-services-with-spring-with/jersey-test-supportを使用して、私は@Autowiredをテストでも使用できるようになりました。 – ebaxt
上記のリンクのように見えますが、リンクが壊れています:http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/ – crobicha