2017-05-22 7 views
0

私はMockito、Springテスト、Hamcrestを使用しています。REST MVCでPOSTテストリクエストを作成する方法Springテスト

私はこのようなユーザーコントローラクラスがあります。

@RestController 
public class UserController { 

private UserResource userResource; 

@RequestMapping(value = "/users", method = RequestMethod.POST) 
public Response create(@Valid @RequestParam String json) throws ResourceException { 
    System.out.println(json); 
    Response response = userResource.insert(json); 
    return response; 
} 

} 

を、これは私のテストです。

java.lang.AssertionError: Status 
Expected :200 
Actual :400 

があることを意味:私はそれを実行したときに

@Test 
public void testPost() throws Exception{ 
    JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); 
    JsonObject user1 = jsonObjectBuilder.add("pseudo", "pseudo1").add("accessToken","accesstoken").add("picture","picture").build(); 
    System.out.println(user1.toString()); 
    User user = new User(); 
    user.setId(1); 
    user.setPseudo("pseudo1"); 
    user.setAccessToken("accesstoken"); 
    user.setPicture("picture"); 
    RestResponse<User> restResponse = new RestResponse<User>(); 
    restResponse.setData(user); 
    Response response = restResponse.throw200Ok(); 

    when(userResource.insert(user1.toString())).thenReturn(response); 
    mockMvc.perform(post("/users").accept(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()) 
      .andExpect(content().contentType("application/json;charset=UTF-8")) 
      .andExpect(jsonPath("$..id").value(1)) 
      .andExpect(jsonPath("$..pseudo").value("pseudo")) 
      .andExpect(jsonPath("$..password").value("password")) 
      .andExpect(jsonPath("$..picture").value("picture")); 


    verify(userResource, times(1)).insert(user1.toString()); 
} 

は、しかし、私のテストが失敗したと私は、このメッセージのエラーを持っている:ユーザーのJSONを取るuserResource.insert私の方法は、ユーザーのインスタンスを再作成し、それを永続化します私は悪い要求をしようとしているが、私はそれまで私のRESTサーバーでテストして成功した。 私のエラーはどこにあるのか分からない。私は前にGETとDELETEテストをエラーなく実行しました。

ログ出力:

mai 22, 2017 11:19:33 AM org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping register 
INFOS: Mapped "{[/users/{id}],methods=[GET]}" onto public 
... 
INFOS: Looking for @ControllerAdvice: org.s[email protected]5f2f57 7 
mai 22, 2017 11:19:33 AM org.springframework.mock.web.MockServletContext log 
INFOS: Initializing Spring FrameworkServlet '' 
mai 22, 2017 11:19:33 AM org.springframework.test.web.servlet.TestDispatcherServlet initServletBean 
INFOS: FrameworkServlet '': initialization started 
mai 22, 2017 11:19:33 AM org.springframework.test.web.servlet.TestDispatcherServlet initServletBean 
INFOS: FrameworkServlet '': initialization completed in 1 ms 
{"pseudo":"pseudo1","accessToken":"accesstoken","picture":"picture"} 

java.lang.AssertionError: Status 
Expected :200 
Actual :400 
<Click to see difference> 


at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54) 
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81) 
... 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
+0

ログファイルの出力を含めることはできますか? – emeraldjava

+0

@emeraldjava私はそれを含んでいます –

答えて

2

私はそのエラーが「承諾」の部分であると考え、「contentTypeの」があるはずです:私はあなたが投稿していない見ることができる限り、また

mockMvc.perform(post("/users").contentType(MediaType.APPLICATION_JSON)).content(some_json_data)... 

"/ users"エンドポイントへの任意のデータ。

+0

あなたが言ったことを試しました。しかし、私は同じエラーメッセージ "400"を何も変更していない。 mockMvc.perform(post( "/ users")。contentType(MediaType.APPLICATION_JSON).content(user1.toString()) –

+0

最後に私はあなたが言ったことを理解して、私は文字列として自分のユーザーを渡すことに成功しました。ありがとうございました –

関連する問題