2011-01-12 6 views
7

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); 
    } 

誰もが正しい方向に私を指すでしたか?

+0

わからない、あなたのテストを維持することが容易になるだろうが、私は、このブログ投稿を見つけた:http://geek.riffpie.com/2010/ 08/09/unit-testing-restful-jersey-services-with-spring-with/jersey-test-supportを使用して、私は@Autowiredをテストでも使用できるようになりました。 – ebaxt

+0

上記のリンクのように見えますが、リンクが壊れています:http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/ – crobicha

答えて

2

説明されている解決策を使用していたのは、hereです。これは問題なく動作しています。

10

私は単純なアプローチを使用していますが、私は多くの場合、あなたの春の豆は、サービス/ダオ層あり、春の豆を使用していますJerseyTestの必要性を理解しない

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); 
    } 
-1

を働き、彼らはユニットテスト/統合テストでなければなりませんMockitoまたはDBUnit(統合テスト)を使用して、レイヤー上に作成します。

私は単位テストを行っています。これは、テストやテストを分離し、JerseyTestではサービスやDao層ではなくJerseyのもの(Json)のみをテストする必要があるためです。私の春の豆のコンテキストを渡しますが、春の豆はモックだけです、私はJerseyTestsで春の豆をテストしたくないためです。あなたはあなたのテストを分離する場合

これを回避する別の方法がある場合、それは書いて

関連する問題