Spring Boot integration test responding with empty body - MockMvc

前端 未结 1 1138
难免孤独
难免孤独 2021-01-11 16:28

I\'ve seen similar questions to this, but I\'ve yet to find a solution that works for me, so i\'m posting it with hopefully enough details to resolve..

So I have the

相关标签:
1条回答
  • 2021-01-11 16:43

    Try with this test. This test is as per spring boot documentation.

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
    @AutoConfigureMockMvc
    public class ControllerTest {
    
    
        @Autowired
        private MockMvc mockMvc;
    
        @MockBean
        private SearchService service;
    
        @Before
        public void setUp(){
            when(service.someMethod(any()))
                    .thenReturn(SomeResponse);
        }
    
        @Test
        public void getSearchResults() throws Exception{
            this.mockMvc.perform(post("/something/search").header("header1","1").header("header2","2")
                    .content("MY VALID JSON REQUEST HERE")
                    .contentType(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andDo(mvcResult -> {
                        //Verrify Response here
                    });
        }
    }
    
    0 讨论(0)
提交回复
热议问题