1

私はこの方法でコントローラーをテストしようとしています:

@RequestMapping(value = "/ test")
public ModelAndView generateRecords(@ModelAttribute( "Employee")Employee employee){

そして、これをテストするためのユニットテストを作成する方法を知りたいです。現在使用しているもの:

MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/test");
//request.setMethod("GET");
new AnnotationMethodHandlerAdapter().handle(request, 
        new MockHttpServletResponse(), this.controller);

このテスト結果を実行すると、ModelAttribute(従業員)の値がNULLになります

統合テストを行うときにmodelattributeオブジェクトをControllerに渡す方法はありますか?

ありがとう


要約すると:

この問題の解決策は、html要素名を選択し、MockHttpRequestオブジェクトにパラメーター値を入力して渡すことです。

例:

MockHttpServletRequest httpServletRequest = MockRequestResponseGenerator.mockRequest(getServletInstance().getServletContext(), "POST", "/test", paramters);

//These paramters must be part of the ModelAttribute Object. Make sure, you are using custom property binding in case you have different object.

        httpServletRequest.setParameter("name", "SPRING MVC INTEGRATION TEST 
TEMP");
        httpServletRequest.setParameter("id", "1");
        httpServletRequest.setParameter("desc", "SPRING MVC INTEGRATION TEST DESC");


        getServletInstance().service(httpServletRequest, httpServletResponse);
4

1 に答える 1

1

OGNLモデル属性/フォームパスに一致するパスに続くパラメーターとして、リクエストの値を設定できます。

于 2011-08-25T15:17:15.173 に答える