Tengo una prueba JUnit que inicia una aplicación Spring-Boot (en mi caso, la clase principal es SpringTestDemoApp
) después de la prueba:
@WebIntegrationTest @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringTestDemoApp.class) public class SpringTest { @Test public void test() { // Test http://localhost:8080/ (with Selenium) } }
Todo funciona bien usando spring-boot 1.3.3.RELEASE
. Sin embargo, la anotación @WebIntegrationTest
y @SpringApplicationConfiguration
se eliminaron en spring-boot 1.5.2.RELEASE
. Intenté refactorizar el código a la nueva versión, pero no puedo hacerlo. Con la siguiente prueba, mi aplicación no se inicia antes de la prueba y http://localhost:8080 devuelve 404:
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringTestDemoApp.class) @WebAppConfiguration public class SpringTest { @Test public void test() { // The same test than before } }
¿Cómo puedo refactorizar mi prueba para que funcione en spring-boot 1.5?
La opción webEnvironment
dentro @SpringBootTest
es muy importante. Puede tomar valores como NONE
, MOCK
, RANDOM_PORT
, DEFINED_PORT
.
NONE
solo creará frijoles de primavera y no se burlará del entorno del servlet.
MOCK
creará frijoles de primavera y un entorno de servlet simulado.
RANDOM_PORT
iniciará el contenedor de servlet real en un puerto aleatorio; esto se puede autoconectar usando @LocalServerPort
.
DEFINED_PORT
tomará el puerto definido en las propiedades e iniciará el servidor con él.
El valor predeterminado es RANDOM_PORT
cuando no define ningún webEnvironment
. Entonces, la aplicación puede estar comenzando en un puerto diferente para usted.
Intente anularlo a DEFINED_PORT
, o intente conectar automáticamente el número de puerto e intente ejecutar la prueba en ese puerto.
No funciona porque SpringBootTest
usa un puerto aleatorio de forma predeterminada, use:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
Este es un fragmento de lo que estoy usando actualmente, por supuesto, dependiendo del controlador web que desee usar, puede crear diferentes beans para él. Asegúrese de tener prueba de arranque de primavera y selenio en su 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>
en mi caso ${selenium.version}
es:
<properties> <selenium.version>2.53.1</selenium.version> </properties>
y esas son las clases:
@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())); } }
¡Espero eso ayude!