I want to write a test for a @NotNull
, @NotEmpty
validation of @ConfigurationProperties
.
@Configuration
@ConfigurationProperties(prefix = "myPrefix", ignoreUnknownFields = true)
@Getter
@Setter
@Validated
public class MyServerConfiguration {
@NotNull
@NotEmpty
private String baseUrl;
}
My Test looks like this:
@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 {
}
}
When I run the test, it reports a failure to start the application before the test is run:
***************************
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:
How can I expect an Exception to write a negative test? Even if I replace the BindException.class
with Throwable.class
the application fails to start.