I have a spring boot actuator and WebMvc test isn't working. It returns an empty body. How would I test this?
@Configuration
@ManagementContextConfiguration
public class TestController extends AbstractMvcEndpoint
{
public TestController()
{
super( "/test", false, true );
}
@GetMapping( value = "/get", produces = MediaType.APPLICATION_JSON_VALUE )
@ResponseBody
public OkResponse getInfo() throws Exception
{
return new OkResponse( 200, "ok" );
}
@JsonPropertyOrder( { "status", "message" } )
public static class OkResponse
{
@JsonProperty
private Integer status;
@JsonProperty
private String message;
public OkResponse(Integer status, String message)
{
this.status = status;
this.message = message;
}
public Integer getStatus()
{
return status;
}
public String getMessage()
{
return message;
}
}
}
When I try to test it with the below, it doesn't work. I get an empty body in the return.
@RunWith( SpringJUnit4ClassRunner.class )
@DirtiesContext
@WebMvcTest( secure = false, controllers = TestController.class)
@ContextConfiguration(classes = {TestController.class})
public class TestTestController
{
private MockMvc mockMvc;
private ObjectMapper mapper;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setup()
{
//Create an environment for it
mockMvc = MockMvcBuilders.webAppContextSetup( this.webApplicationContext )
.dispatchOptions( true ).build();
mapper = new ObjectMapper();
}
@SpringBootApplication(
scanBasePackageClasses = { TestController.class }
)
public static class Config
{
}
@Test
public void test() throws Exception
{
//Get the controller's "ok" message
String response = mockMvc.perform(
get("/test/get")
).andReturn().getResponse().getContentAsString();
//Should be not null
Assert.assertThat( response, Matchers.notNullValue() );
//Should be equal
Assert.assertThat(
response,
Matchers.is(
Matchers.equalTo(
"{\"status\":200,\"message\":\"ok\"}"
)
)
);
}
}