What is the best way to test that a spring application context fails to start?

谁说胖子不能爱 提交于 2019-12-19 10:09:14

问题


I use the spring-boot-starter-web and spring-boot-starter-test.

Let's say I have a class for binding configuration properties:

@ConfigurationProperties(prefix = "dummy")
public class DummyProperties {

    @URL
    private String url;

    // getter, setter ...

}

Now I want to test that my bean validation is correct. The context should fail to start (with a specfic error message) if the property dummy.value is not set or if it contains an invalid URL. The context should start if the property contains a valid URL. (The test would show that @NotNull is missing.)

A test class would look like this:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@IntegrationTest({ "dummy.url=123:456" })
public class InvalidUrlTest {
    // my test code
}

This test would fail because the provided property is invalid. What would be the best way to tell Spring/JUnit: "yep, this error is expected". In plain JUnit tests I would use the ExpectedException.


回答1:


Why is that an integration test to begin with? Why are you starting a full blown Spring Boot app for that?

This looks like unit testing to me. That being said, you have several options:

  • Don't add @IntegrationTest and Spring Boot will not start a web server to begin with (use @PropertySource to pass value to your test but it feels wrong to pass an invalid value to your whole test class)
  • You can use spring.main.web-environment=false to disable the web server (but that's silly given the point above)
  • Write a unit test that process that DummyProperties of yours. You don't even need to start a Spring Boot application for that. Look at our own test suite

I'd definitely go with the last one. Maybe you have a good reason to have an integration test for that?




回答2:


I think the easiest way is:

public class InvalidUrlTest {

    @Rule
    public DisableOnDebug testTimeout = new DisableOnDebug(new Timeout(5, TimeUnit.SECONDS));
    @Rule
    public ExpectedException expected = ExpectedException.none();

    @Test
    public void shouldFailOnStartIfUrlInvalid() {
        // configure ExpectedException
        expected.expect(...

        MyApplication.main("--dummy.url=123:456");
    }

// other cases
}


来源:https://stackoverflow.com/questions/31692863/what-is-the-best-way-to-test-that-a-spring-application-context-fails-to-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!