小编Tan*_*MUC的帖子

Spring Boot @ConfigurationProperties 验证测试失败

我想编写一个测试来@NotNull验证.@NotEmpty@ConfigurationProperties

@Configuration
@ConfigurationProperties(prefix = "myPrefix", ignoreUnknownFields = true)
@Getter
@Setter
@Validated
public class MyServerConfiguration {
  @NotNull
  @NotEmpty
  private String baseUrl;
}
Run Code Online (Sandbox Code Playgroud)

我的测试如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest()
public class NoActiveProfileTest {
  @Test(expected = org.springframework.boot.context.properties.bind.validation.BindValidationException.class)
  public void should_ThrowException_IfMandatoryPropertyIsMissing() throws Exception {
  }
Run Code Online (Sandbox Code Playgroud)

}

当我运行测试时,它报告在测试运行之前启动应用程序失败:

***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target   org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'myPrefix' to com.xxxxx.configuration.MyServerConfiguration$$EnhancerBySpringCGLIB$$4b91954c failed:
Run Code Online (Sandbox Code Playgroud)

我如何期望异常会编写负面测试?即使我将其替换BindException.class为Throwable.class应用程序也无法启动。

spring-boot spring-boot-test

5
推荐指数
1
解决办法
1889
查看次数

标签 统计

spring-boot ×1

spring-boot-test ×1