【问题标题】:Test the SpringBoot application startup测试 Spring Boot 应用启动
【发布时间】: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 中工作?

【问题讨论】:

  • 您能在日志中看到任何异常/消息吗?

标签: java spring spring-boot junit spring-test


【解决方案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()));
        }
    }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2017-04-14
      • 2018-08-06
      • 2018-09-05
      相关资源
      最近更新 更多