Использование postForEntity(url, null, ResponseType.class)
работает для меня.
Мое пост-отображение равно вашему, за исключением responseType. Я использовал Map
просто в качестве примера
@PostMapping(value = "/customers/{customerId}")
public Map<String, String> manageCustomers(@PathVariable String customerId){
return new HashMap<String, String>(){{ put("customerId", customerId); }};
}
Тест, чтобы убедиться, что он работает
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CustomerControllerTest {
@LocalServerPort
private int port;
private final TestRestTemplate testRestTemplate = new TestRestTemplate();
@Test
public void postEmptyBodyShouldReturn200OK() {
String customerId = "123";
ResponseEntity responseEntity = testRestTemplate
.postForEntity(format("http://localhost:%s/ping/customers/%s", port, customerId), null, Map.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON_UTF8);
assertThat(responseEntity.getBody()).isNotNull();
assertThat(((LinkedHashMap) responseEntity.getBody()).size()).isEqualTo(1);
assertThat(((LinkedHashMap) responseEntity.getBody()).get("customerId")).isEqualTo(customerId);
}
}
Выполнение этого теста в maven
$ mvn -Dtest=CustomerControllerTest test
(...removed unnecessary output...)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.137 s - in com.ins.example.demo.rest.CustomerControllerTest
10:04:54.683 [Thread-3] INFO o.s.s.c.ThreadPoolTaskExecutor - Shutting down ExecutorService 'applicationTaskExecutor'
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.940 s
[INFO] Finished at: 2019-04-07T10:04:55+02:00
[INFO] ------------------------------------------------------------------------