Test the SpringBoot application startup

13,444

Solution 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.

Solution 2

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

Solution 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!

Share:
13,444
Boni García
Author by

Boni García

I have a PhD in Information and Communications Technology from the Technical University of Madrid (UPM) in Spain since 2011. I work as a Visiting Professor at Universidad Carlos III de Madrid (UC3M) in Spain. My main research interest is software engineering, with a special focus on automated testing. Also, I am Staff Software Engineer at Sauce Labs, working in the Open Source Program Office. I am the author of more than 45 research papers in different journals, magazines, and international conferences. I wrote the books Mastering Software Testing with JUnit 5 (Packt Publishing, 2017) and Hands-On Selenium WebDriver with Java (O'Reilly Media, 2022). I am the creator and maintainer of several open-source projects related to Selenium, such as WebDriverManager, Selenium-Jupiter, and BrowserWatcher.

Updated on June 15, 2022

Comments

  • Boni García
    Boni García almost 2 years

    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?

  • Scott
    Scott about 6 years
    The default is MOCK, at least with Spring Boot 1.5.10.