0

I try to integrate Unittests in my App and fail on testing an PUT(JSON) REST API.

Test Code:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class EventOrderRestTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    ObjectMapper objectMapper;

    private Integer id;


    @Test
    public void a_saveNewEventOrder() throws Exception {

        EventOrder o = new EventOrder();
        o.setPlz(54321);

        this.id = Integer.parseInt(
                mvc.perform(put("/order")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(o))
                        .accept(MediaType.TEXT_PLAIN))
                    .andExpect(status().isOk())
                    .andReturn().getResponse().getContentAsString()
        );

    }

I have no other Test Configurations in my App.

So while running the API and calling it manually, it returns a code 200 and the new ObjectId.

While the test is running with same value in body it returns a code 406.

Whats wrong with it? What did I miss?

Gregor Sklorz
  • 1,645
  • 2
  • 19
  • 27

2 Answers2

1

406 HTTP Status - What is "406-Not Acceptable Response" in HTTP?

Maybe this is the cause of problem.

.accept(MediaType.TEXT_PLAIN))
Min Hyoung Hong
  • 1,102
  • 9
  • 13
1

The method in the controller which is responsible for put /order endpoint should return String if you want to get plain text. The method should look like the following:

@PutMapping(path="/order")
public @ResponseBody String putOrder() { 
   ...
   return "result";
}
Sergey Sargsyan
  • 452
  • 6
  • 10