Si necesitas hacer test de la pagina que estas creando, esta libreria te puede ser de gran ayuda. En un navegador sin interfaz gráfica, solo código, con un compilador de javascript.
test
TestNG test parametrizados
Hay dos maneras, parametrizando en la clase(@DataProvider) o ejecutando un suite(testng.xml)
Parametrizando en la clase @DataProvider
@ContextConfiguration(locations = { "classpath:spring-test.xml", "classpath:spring/spring-client.xml" }) public class ConsultaIdentidadEnPaxaseTest extends AbstractTestNGSpringContextTests { @DataProvider(name = "ficheros") public Object[][] consultarIdentidad_peticionProvider() { return new Object[][]{ {String.class, new String("src/test/resources/peticiones/Peticion1.xml")}, {String.class, new String("src/test/resources/peticiones/Peticion2.xml")}, }; } @Test(dataProvider = "ficheros") public void consultarIdentidad_peticion(Class<?> clzz, String fichero) { consultarIdentidad_peticion(fichero); } }
Ejecutando un fichero
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="cliente_consultaIdentidad" preserve-order="true"> <test name="cliente_Peticion1"> <parameter name="fichero" value="src/test/resources/peticiones/Peticion1.xml" /> <classes> <class name="es.una.ruta.services.spi.ConsultaIdentidadEnPaxaseTest"></class> </classes> </test> <test name="cliente_Peticion2"> <parameter name="fichero" value="src/test/resources/peticiones/Peticion2.xml" /> <classes> <class name="es.una.ruta.services.spi.ConsultaIdentidadEnPaxaseTest"></class> </classes> </test> </suite>
@ContextConfiguration(locations = { "classpath:spring-test.xml", "classpath:spring/spring-client.xml" }) public class ConsultaIdentidadEnPaxaseTest extends AbstractTestNGSpringContextTests { @Autowired @Qualifier("clientCI") private es.una.ruta.services.ConsultaIdentidad clientCI; @Parameters({ "fichero" }) @Test public void consultarIdentidad_peticion(String fichero){ try { String peticion = getPeticion(fichero); String respuesta = clientCI.consultar(peticion); System.out.println(respuesta); assertNotNull("Respuesta null", respuesta); } catch (Exception e) { assertTrue(false, fichero + ": " +e.getMessage()); } } private String getPeticion(String path){ String peticion = null; try { File file = new File(path); peticion = FileUtils.readFileToString(file, "UTF-8"); System.out.println(peticion); } catch (Exception e) { e.printStackTrace(); } return peticion; } }
<build> <plugins> <plugin> <!-- mvn test --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build>
testNG
Del ultimo post, hay un test con JUnit.
El mismo test pero con TestNG:
<!-- en el pom.xml quitar la dependencia con JUnit y añadir--> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.5</version> <scope>test</scope> </dependency>
package net.prietopalacios.josemanuel.aop.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.Test; import static org.testng.Assert.*; @ContextConfiguration("file:src/main/resources/spring.xml") public class ServiceImplTest extends AbstractTestNGSpringContextTests { @Autowired Service service; @Test(threadPoolSize = 2, invocationCount = 2, timeOut = 10000) // @Test(threadPoolSize = 2, invocationCount = 4) public void testDoSomething() { try { service.sendSomething(); assertTrue(true); } catch (Exception e) { assertException(e); } } @Test(threadPoolSize = 2, invocationCount = 2, timeOut = 10000) public void testThrowEsception() { try { service.sendEsception(); assertTrue(true); } catch (Exception e) { assertException(e); } } private void assertException(Exception e) { if(e.getMessage() != null){ assertTrue(false, e.getMessage()); }else{ assertTrue(false, e.getCause().getMessage()); } } }