我想编写一个测试来@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应用程序也无法启动。