Don*_*mmy 5 java spring spring-mvc spring-boot spring-boot-actuator
我有一个弹簧启动执行器,但 WebMvc 测试不起作用。它返回一个空的主体。我将如何测试这个?
@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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下内容对其进行测试时,它不起作用。我得到了一个空的身体。
@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\"}"
)
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我编写的一个测试,用于执行类似于您所说的操作。此测试验证我们想要公开的执行器端点是否可用。
这是使用 SpringBoot 1.5。
我发现这里的问题很有帮助:Unit test of Spring Boot Actuator Endpoints not work when指定端口。
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = {
"management.port="
})
public class ActuatorTests {
@Autowired
private WebApplicationContext context;
@Autowired
private FilterChainProxy springSecurityFilterChain;
private MockMvc mvc;
@Before
public void setup() {
context.getBean(MetricsEndpoint.class).setEnabled(true);
mvc = MockMvcBuilders
.webAppContextSetup(context)
.alwaysDo(print())
.apply(SecurityMockMvcConfigurers.springSecurity(springSecurityFilterChain))
.build();
}
@Test
public void testHealth() throws Exception {
MvcResult result = mvc.perform(get("/health")
.with(anonymous()))
.andExpect(status().is2xxSuccessful())
.andReturn();
assertEquals(200, result.getResponse().getStatus());
}
@Test
public void testRestart() throws Exception {
MvcResult result = mvc.perform(get("/restart")
.with(anonymous()))
.andExpect(status().is3xxRedirection())
.andReturn();
assertEquals(302, result.getResponse().getStatus());
assertEquals("/sso/login", result.getResponse().getRedirectedUrl());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5874 次 |
| 最近记录: |