2012-04-02 13 views
0

私はポートレットを使った初心者学習スプリングMVCです。私はビューを返すコントローラを持っています。私はコントローラの単体テストを書く方法がわかりません。ユニットテストアノテーションベースのSpring MVCポートレットコントローラ

@controller 
@RequestMapping("VIEW") 
public class HelloController { 

@ResourceMapping(value = "hello") 
public String helloWorld(RenderRequest request) { 

    return "hello"; 
} 

と私のユニットテストコントローラは、これは結果として機能していません。この

public class HelloWorldControllerTest extends TestCase { 

protected void setUp() throws Exception { 
    super.setUp(); 
} 

protected void tearDown() throws Exception { 
    super.tearDown(); 
} 

public void testHelloWorldController() throws IOException { 

      MockRenderRequest request = new MockRenderRequest(); 
     HelloController c = new HelloController(); 
     ModelAndView result = c.helloWorld(request); 
     assertNotNull("ModelAndView should not be null", result); 
     assertEquals("hello", result.getViewName()); 
} 

のようなものであるがのModelAndViewオブジェクトではありませんが、それはコントローラ内の文字列です。戻り値の型は、メインコントローラ内のModelAndViewオブジェクトにすることができますが、Springアノテーションを使用している場合は、戻り値の型が文字列であることがわかります。誰もがベストプラクティスか、私が理解に間違っていると示唆することができます。事前

答えて

1

おかげでspring-test-mvcプロジェクトは、Spring MVCのコントローラのテストを容易にします。