Spring boot Spring Boot@ConfigurationProperties验证测试失败
我想为Spring boot Spring Boot@ConfigurationProperties验证测试失败,spring-boot,spring-boot-test,Spring Boot,Spring Boot Test,我想为@NotNull,@NotEmpty验证@ConfigurationProperties编写一个测试 @Configuration @ConfigurationProperties(prefix = "myPrefix", ignoreUnknownFields = true) @Getter @Setter @Validated public class MyServerConfiguration { @NotNull @NotEmpty private String bas
@NotNull
,@NotEmpty
验证@ConfigurationProperties
编写一个测试
@Configuration
@ConfigurationProperties(prefix = "myPrefix", ignoreUnknownFields = true)
@Getter
@Setter
@Validated
public class MyServerConfiguration {
@NotNull
@NotEmpty
private String baseUrl;
}
我的测试如下所示:
@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 {
}
}
运行测试时,它会报告在运行测试之前启动应用程序失败:
***************************
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:
我怎么能期望异常写入否定测试?即使我将
BindException.class
替换为Throwable.class
应用程序也无法启动。尝试以编程方式加载Spring Boot应用程序上下文:
简单版
公共类AppFailIT{
@试验
公共void testFail(){
试一试{
新的注释ConfigServletWebServerApplicationContext(MyApplication.class);
}
捕获(例外e){
assertThat(e).isInstanceOf(unsatifiedPendencyException.class);
Assertions.assertThat(e.getMessage()).contains(“嵌套异常为org.springframework.boot.context.properties.bind.BindException:绑定属性失败”);
返回;
}
失败();
}
}
扩展版
能够从application-test.properties加载环境,并在方法级别向测试环境添加自己的键:值:
@TestPropertySource(“classpath:application test.properties”)
公共类AppFailIT{
@统治
公共最终SpringMethodRule SpringMethodRule=新的SpringMethodRule();
@自动连线
私有可配置环境可配置环境;
@试验
公共void testFail(){
试一试{
MockEnvironment MockEnvironment=新建MockEnvironment();
mockEnvironment.withProperty(“a”、“b”);
可配置环境。合并(mockEnvironment);
AnnotationConfigServletWebServerApplicationContext applicationContext=新的AnnotationConfigServletWebServerApplicationContext();
applicationContext.setEnvironment(configurableEnvironment);
applicationContext.register(MyApplication.class);
applicationContext.refresh();
}
捕获(例外e){
Assertions.assertThat(e.getMessage()).contains(“嵌套异常为org.springframework.boot.context.properties.bind.BindException:绑定属性失败”);
返回;
}
失败();
}
}
我会为此使用应用程序ContextRunner
,例如
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
ConfigurationPropertiesAutoConfiguration.class,
ValidationAutoConfiguration.class
))
.withUserConfiguration(MyServerConfiguration.class)
.withPropertyValues("foo=bar")
.run(context -> {
var error = assertThrows(IllegalStateException.class, () -> context.getBean(MyServerConfiguration.class));
var validationError = (BindValidationException) ExceptionUtils.getRootCause(error);
var fieldViolation = (FieldError) validationError.getValidationErrors().iterator().next();
var fieldInError = fieldViolation.getObjectName() + "." + fieldViolation.getField();
assertThat(fieldInError, is(expectedFieldInError));
assertThat(fieldViolation.getDefaultMessage(), is(expectedMessage));
});