Test the SpringBoot application startup

喜夏-厌秋 提交于 2020-12-30 08:02:43

问题


I have a JUnit test that starts an spring-boot application (in my case, the main class is SpringTestDemoApp) after the test:

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

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

Everything works fine using spring-boot 1.3.3.RELEASE. Nevertheless, the annotation @WebIntegrationTest and @SpringApplicationConfiguration have been removed in spring-boot 1.5.2.RELEASE. I tried to refactor the code to the new version, but I am not able to do it. With the following test, my app is not started before the test and http://localhost:8080 returns 404:

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

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

}

How can I refactor my test to make it works in spring-boot 1.5?


回答1:


The webEnvironment option inside @SpringBootTest is very important. It can take values like NONE, MOCK, RANDOM_PORT, DEFINED_PORT.

  • NONE will only create spring beans and not any mock the servlet environment.

  • MOCK will create spring beans and a mock servlet environment.

  • RANDOM_PORT will start the actual servlet container on a random port; this can be autowired using the @LocalServerPort.

  • DEFINED_PORT will take the defined port in the properties and start the server with it.

The default is RANDOM_PORT when you don’t define any webEnvironment. So the app may be starting at a different port for you.

Try to override it to DEFINED_PORT, or try to autowire the port number and try to run test on that port.




回答2:


It does not work because SpringBootTest uses random port by default, please use:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)



回答3:


This is a snippet of what I'm currently using, of course depending on the web-driver you want to use you can create different beans for it. Make sure you have spring boot test and selenium on your 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>

in my case ${selenium.version} is:

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

and those are the classes:

@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()));
    }
}

hope it helps!



来源:https://stackoverflow.com/questions/43281370/test-the-springboot-application-startup

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