测试 Spring Boot 应用启动

Posted

技术标签:

【中文标题】测试 Spring Boot 应用启动【英文标题】:Test the SpringBoot application startup 【发布时间】:2017-09-03 00:43:15 【问题描述】:

我有一个 JUnit 测试,它在测试后启动一个 spring-boot 应用程序(在我的例子中,主类是 SpringTestDemoApp):

@WebIntegrationTest
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringTestDemoApp.class)
public class SpringTest 

    @Test
    public void test() 
        // Test http://localhost:8080/ (with Selenium)
    

使用 spring-boot 1.3.3.RELEASE 一切正常。尽管如此,注释 @WebIntegrationTest@SpringApplicationConfiguration 已在 spring-boot 1.5.2.RELEASE 中删除。我试图将代码重构为新版本,但我无法做到。通过以下测试,我的应用在测试之前没有启动,http://localhost:8080 返回 404:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringTestDemoApp.class)
@WebAppConfiguration
public class SpringTest 

    @Test
    public void test() 
        // The same test than before
    


如何重构我的测试以使其在 spring-boot 1.5 中工作?

【问题讨论】:

您能在日志中看到任何异常/消息吗? 【参考方案1】:

@SpringBootTest 中的webEnvironment 选项非常重要。它可以采用NONEMOCKRANDOM_PORTDEFINED_PORT 等值。

NONE 只会创建 spring bean 而不会模拟 servlet 环境。

MOCK 将创建 spring bean 和模拟 servlet 环境。

RANDOM_PORT 将在随机端口上启动实际的 servlet 容器;这可以使用@LocalServerPort 自动装配。

DEFINED_PORT 将采用属性中定义的端口并使用它启动服务器。

当您没有定义任何webEnvironment 时,默认值为RANDOM_PORT。因此,该应用程序可能会在您的不同端口启动。

尝试将其覆盖为DEFINED_PORT,或尝试自动连接端口号并尝试在该端口上运行测试。

【讨论】:

默认是 MOCK,至少 Spring Boot 1.5.10 是这样。【参考方案2】:

因为SpringBootTest默认使用随机端口所以不起作用,请使用:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

【讨论】:

【参考方案3】:

这是我目前正在使用的一个 sn-p,当然,根据您要使用的网络驱动程序,您可以为其创建不同的 bean。 确保您的pom.xml 上有弹簧启动测试和硒:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>$selenium.version</version>
        <scope>test</scope>
    </dependency>

在我的情况下 $selenium.version 是:

<properties>
    <selenium.version>2.53.1</selenium.version>
</properties>

这些是类:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(IntegrationConfiguration.class)
public abstract class AbstractSystemIntegrationTest 

    @LocalServerPort
    protected int serverPort;

    @Autowired
    protected WebDriver driver;

    public String getCompleteLocalUrl(String path) 
        return "http://localhost:" + serverPort + path;
    


public class IntegrationConfiguration 

    @Bean
    private WebDriver htmlUnitWebDriver(Environment env) 
        return new HtmlUnitDriver(true);
    



public class MyWhateverIT extends AbstractSystemIntegrationTest 

    @Test
    public void myTest() 
        driver.get(getCompleteLocalUrl("/whatever-path/you/can/have"));
        WebElement title = driver.findElement(By.id("title-id"));
        Assert.assertThat(title, is(notNullValue()));
    

希望对你有帮助!

【讨论】:

以上是关于测试 Spring Boot 应用启动的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot web 单元测试程序

002-启动spring boot应用

在黄瓜测试模块中使用主应用程序数据源 - Spring-boot 应用程序

Spring Boot 测试不启动上下文或加载依赖项

Spring Boot 单元测试示例

Spring Boot中多war应用的集成测试