2

Spring MVC は初めてですが、Spring MVC と resteasy を使用して Web サービスを作成しました。私のコントローラーは正常に動作しており、テストケースを作成する必要がありますが、書き込みを試みましたが、オートワイヤリングで問題が発生することはありませんでした。

@コントローラ

@Path("/searchapi")
public class SearchAPIController は ISearchAPIController を実装します {
 @Autowired
    プライベート ISearchAPIService srchapiservice;
@得る
    @Path("/{ドメイン}/{グループ}/検索")
    @Produces({"アプリケーション/xml", "アプリケーション/json"})
    public Collections getSolrData(
            @PathParam("domain") final 文字列ドメイン、
            @PathParam("group") 最終文字列グループ、
            @Context final UriInfo uriinfo) throws Exception {    
       System.out.println("LANDED IN get****************");
        return srchapiservice.getData(ドメイン、グループ、uriinfo);
    }
}

spring mvc のテスト ケースのサンプル コードを教えてください。

4

2 に答える 2

4

「Spring-MVC」テスト ケースは、たとえば MyControllerToBeTest をテストしたい場合など、モック オブジェクトを使用して次のようになります。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring.xml")
public class MyControllerTest {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private MyControllerToBeTested controller;
    private AnnotationMethodHandlerAdapter adapter;

    @Autowired
    private ApplicationContext applicationContext;

    @Before
    public void setUp() {
        request    = new MockHttpServletRequest();
        response   = new MockHttpServletResponse();
        response.setOutputStreamAccessAllowed(true);
        controller = new MyControllerToBeTested();
        adapter = new AnnotationMethodHandlerAdapter();
    }

    @Test
    public void findRelatedVideosTest() throws Exception {
        request.setRequestURI("/mypath");
        request.setMethod("GET");
        request.addParameter("myParam", "myValue");
        adapter.handle(request, response, controller);
        System.out.println(response.getContentAsString());
    }

}

しかし、RESTリソースのテストの経験はありません。あなたの場合はRestEasyです。

于 2011-05-11T12:59:55.817 に答える
0

コンテナー内の完全なサービスをテストしたい場合は、REST Assuredフレームワーク for Java を参照してください。HTTP/REST ベースのサービスのテストと検証が非常に簡単になります。

于 2012-03-01T07:19:03.790 に答える