• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

96
Vistas
Test the SpringBoot application startup

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?

almost 3 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.

almost 3 years ago · Santiago Trujillo Denunciar

0

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
almost 3 years ago · Santiago Trujillo Denunciar

0

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!

almost 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda