2012-05-17 28 views
7

私はSpringの依存関係をSpring 3.1.1.RELEASEにアップグレードしましたが、私はspring-test-mvcを単体コントローラの単体テストに使用しようとしています。私はSpring REST Controller Test with spring-test-mvc frameworkで使用されているテクニックに従っています。なぜなら、その人のために働いたように思われるからですが、私はこれまでに失敗しています。私は、「私のテストコンテキストファイルに欠落しているmはいくつかの重要な設定があると思う。ユニットテストREST Controller with spring-test-mvc

私はエラーを取得していない。Hello Worldが印刷されることは決してありませんので、私はそれが動作していないことを知っている理由は、(コントローラを参照してください)。私はここで何をしないのですか?

コントローラー:

@Controller 
@RequestMapping("/debug") 
public class DebugOutputController { 

    @RequestMapping(method = RequestMethod.POST) 
    public void saveDebugOutput(@RequestBody DebugOutput debugOutput, HttpServletResponse response) { 
     System.out.println("Hello World"); 
    } 
} 

テストクラス:

@RunWith(SpringJUnit4ClassRunner.class) //this lets tests access Spring beans defined in the context config file 
@ContextConfiguration(locations={"file:src/test/resources/itest/restAPITestContext.xml"}) //tells the test where to get configuration and beans to be used by the test. 
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) //overrides the default stack of listeners 
public class ITRestAPI{ 

@Autowired 
private DebugOutputController debugOutputController; 

private MockMvc mockMvc; 

@Before 
public void setUp() throws Exception { 
    mockMvc = MockMvcBuilders.standaloneSetup(debugOutputController).build(); 
} 

@After 
public void tearDown() throws Exception { 
} 

@Test 
public void shouldPerformPost() throws Exception { 
    this.mockMvc.perform(post("/debug")); 
} 
} 

restAPITestContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <mvc:annotation-driven /> 
    <mvc:default-servlet-handler /> 
    <context:component-scan resource-pattern="*DebugOutputController*" base-package="com.company.project.servlet" />  

</beans> 
+0

spring-test-mvcは本当に有望ですが、ドキュメントがありません。この時点ではREADME以外何も認識していますか? –

+0

@MikePartridge私が発見したすべての情報はGithubサイトからのものです。 –

答えて

14

HttpMessageNotReadable例外が発生したと私はどこでもそれをログに記録するか、印刷していなかったので、私はそれを見ることができなかったが判明。私はDefaultRequestBuilderクラスを使用して、私のテストクラスでHTTPリクエストを構築し、追加することによって、それを見つけたandDo(print())

DefaultRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/debug").contentType(MediaType.APPLICATION_JSON).body(new String("{\"T1\":109.1, \"T2\":99.3}").getBytes()); 
this.mockMvc.perform(requestBuilder).andDo(print()); 

だから後、andDo(print())の出力を使用して、私は、HttpMessageNotReadable例外がスローされていたことを見ることができましたが、例外の詳細や原因を知らなかった。詳細を参照するには、私は、レスポンスボディに例外の詳細を記述するためにコントローラクラスにこれを追加する必要がありました:

@ExceptionHandler(HttpMessageNotReadableException.class) 
@ResponseBody 
public String handleException1(HttpMessageNotReadableException ex) 
{ 
    return ex.getMessage(); 
} 

これは、次の例外が明らかになった:

私は追加することにより、固定
Could not read JSON: Unrecognized field "T1" (Class com.company.project.model.device.DebugOutput), not marked as ignorable 

私のモデルクラスのセッターへ@JsonProperty注釈:

@JsonProperty("T1") 
public void setT1(Float t1) { 
    T1 = t1; 
} 
+0

あなたが言ったこの "print()"メソッドは何ですか?テストクラスで見つけることができません。テストクラスはどこからでも拡張されていないため、どこから取得したのか、どこから取得したのかを指摘できますか?ありがとう。 –

+2

@MathiasLin私はprint()メソッドを静的にインポートして使用します: 'import static org.springframework.test.web.server.result.MockMvcResultHandlers.print;'このメソッドは、送信されたリクエストに関する詳細を出力します。これはデバッグに非常に便利です。 –

+3

は、実際のimport文が以下の通りであることを指摘しなければなりません: 'import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print' – leojh

0

春試験-MVCアーティファクトのgood presentationは、以下のプレゼンテーションの最後にある、私はtは文書のページ116の周りから始まります。

関連する問題