9

Spring 依存関係を Spring 3.1.1.RELEASE にアップグレードしました。単純なコントローラーの単体テストにspring-test-mvcを使用しようとしています。Spring REST Controller Test で使用されている手法を 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>
4

1 に答える 1

17

例外が発生していたことが判明しHttpMessageNotReadableましたが、ログに記録したり印刷したりしていなかったため、それを見ることができませんでした。クラスを使用してテストクラスでHTTPリクエストを作成し、次DefaultRequestBuilderを追加することでそれを見つけました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;
}
于 2012-05-18T14:26:52.030 に答える