这是我第一次尝试创建示例 spring boot 反应式应用程序。我无法对执行器运行状况端点进行单元测试。我得到的响应代码是 401 而不是 200。尽管如果我卷曲到终点或在浏览器中打开它,它打开时没有任何错误。
请帮忙
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-reactive</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-reactive</name>
<description>spring-reactive</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
单元测试是:
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@WebAppConfiguration
@ContextConfiguration(classes=SecurityTestConfig.class)
class HealthCheckTest {
@Autowired
private MockMvc mockMvc;
@Test
void healthTest() throws Exception {
mockMvc.perform(get("/actuator/health"))
.andDo(print())
.andExpect(status().isOk());
}
}
测试资源下的application.yml是
management:
endpoint:
health:
enabled: true
server:
port: 8080
@Configuration
@EnableWebFluxSecurity 公共类 SecurityTestConfig {
@Bean
public SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
return http.authorizeExchange()
.anyExchange()
.permitAll()
.and().build();
}
}
面临的错误是:
java.lang.AssertionError:预期状态:<200>但为:<401> 预期:200 实际:401