Commit 08da552f by Antonio Rueda

Actualizado testing mediante base de datos para testing.

parents bea00abe 9ef7b48f
...@@ -16,10 +16,17 @@ Después arranca el contenedor, define _secret_ como clave de root y ...@@ -16,10 +16,17 @@ Después arranca el contenedor, define _secret_ como clave de root y
asocia MySQL al puerto de la máquina anfitrión 33060. asocia MySQL al puerto de la máquina anfitrión 33060.
``` ```
docker exec mysql-db mysql -psecret -e create database ujacoin; use ujacoin; create user 'ujacoin' identified by 'secret'; grant all privileges on ujacoin.* to 'ujacoin'@'%'" docker exec mysql-db mysql -psecret -e "create database ujacoin; use ujacoin; create user 'ujacoin' identified by 'secret'; grant all privileges on ujacoin.* to 'ujacoin'@'%'"
``` ```
Este comando ejecuta la utilidad de administración `mysql` dentro del contenedor, Este comando ejecuta la utilidad de administración `mysql` dentro del contenedor,
crea la base de datos *ujacoin*, un usuario con el mismo nombre y clave _secret_ crea la base de datos *ujacoin*, un usuario con el mismo nombre y clave _secret_
y finalmente le otorga los permisos necesarios para trabajar con la base y finalmente le otorga los permisos necesarios para trabajar con la base
de datos. de datos.
Para el testing, crear una nueva base de datos ujacoin_test y dar permisos al usuario creado anteriormente.
```
docker exec mysql-db mysql -psecret -e "create database ujacoin_test; use ujacon_test; grant all privileges on ujacoin_test.* to 'ujacoin'@'%'"
```
...@@ -45,6 +45,12 @@ ...@@ -45,6 +45,12 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId> <artifactId>spring-boot-starter-validation</artifactId>
</dependency> </dependency>
......
## Fichero de configuración para UjaCoin durante testing
# spring.datasource.url: jdbc:mysql://localhost:33060/ujacoin_test
spring.datasource.url: jdbc:h2:mem:ujacoin_test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1
spring.jpa.properties.javax.persistence.schema-generation.database.action: drop-and-create
## YAML Template. ## Fichero de configuración para UjaCoin
spring.datasource.url: jdbc:mysql://localhost:33060/ujacoin spring.datasource.url: jdbc:mysql://localhost:33060/ujacoin
spring.datasource.username: ujacoin spring.datasource.username: ujacoin
......
package es.ujaen.dae.ujacoin.servicios;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
/**
* Servicio auxiliar de borrado de los datos en la base de datos (sólo para testing)
* @author ajrueda
*/
@Service
public class ServicioLimpiadoBaseDatos {
@PersistenceContext
EntityManager em;
@Autowired
TransactionTemplate transactionTemplate;
/**
* Lista de entidades a borrar. Ojo: el orden es muy importante
* para evitar errores de violación de integridad
*/
final String[] entidades = {
"Movimiento",
"Cuenta",
"Tarjeta",
"Cliente"
};
final String deleteFrom = "delete from ";
/** Realizar borrado */
public void limpiar() {
transactionTemplate.executeWithoutResult(transactionStatus -> {
for (String tabla : entidades) {
em.createQuery(deleteFrom + tabla).executeUpdate();
}
});
}
}
...@@ -18,10 +18,12 @@ import java.util.concurrent.Executors; ...@@ -18,10 +18,12 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.MethodMode;
import org.springframework.test.context.ActiveProfiles;
/** /**
* Test de integración de la aplicación * Test de integración de la aplicación
...@@ -29,14 +31,12 @@ import org.springframework.boot.test.context.SpringBootTest; ...@@ -29,14 +31,12 @@ import org.springframework.boot.test.context.SpringBootTest;
*/ */
// @Disabled // @Disabled
@SpringBootTest(classes = es.ujaen.dae.ujacoin.app.UjaCoinApp.class) @SpringBootTest(classes = es.ujaen.dae.ujacoin.app.UjaCoinApp.class)
@ActiveProfiles(profiles = {"test"})
public class ServicioUjaCoinTest { public class ServicioUjaCoinTest {
@Autowired @Autowired
ServicioUjaCoin servicioUjaCoin; ServicioUjaCoin servicioUjaCoin;
@Autowired
ServicioLimpiadoBaseDatos limpiadorBaseDatos;
@Test @Test
public void testAccesoServicioUjaCoin() { public void testAccesoServicioUjaCoin() {
Assertions.assertThat(servicioUjaCoin).isNotNull(); Assertions.assertThat(servicioUjaCoin).isNotNull();
...@@ -60,6 +60,7 @@ public class ServicioUjaCoinTest { ...@@ -60,6 +60,7 @@ public class ServicioUjaCoinTest {
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
public void testAltaYLoginClienteCuenta() { public void testAltaYLoginClienteCuenta() {
Cliente cliente = new Cliente( Cliente cliente = new Cliente(
"11995667D", "11995667D",
...@@ -79,6 +80,7 @@ public class ServicioUjaCoinTest { ...@@ -79,6 +80,7 @@ public class ServicioUjaCoinTest {
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
public void testCreacionCuentaAdicional() { public void testCreacionCuentaAdicional() {
Cliente cliente = new Cliente( Cliente cliente = new Cliente(
"11995667D", "11995667D",
...@@ -101,6 +103,7 @@ public class ServicioUjaCoinTest { ...@@ -101,6 +103,7 @@ public class ServicioUjaCoinTest {
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
public void testAnadirTarjetaACliente() { public void testAnadirTarjetaACliente() {
// Registrar cliente y realizar login // Registrar cliente y realizar login
Cliente cliente = new Cliente( Cliente cliente = new Cliente(
...@@ -124,6 +127,7 @@ public class ServicioUjaCoinTest { ...@@ -124,6 +127,7 @@ public class ServicioUjaCoinTest {
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
public void testIngreso() { public void testIngreso() {
// Registrar cliente y realizar login // Registrar cliente y realizar login
Cliente cliente = new Cliente( Cliente cliente = new Cliente(
...@@ -157,6 +161,7 @@ public class ServicioUjaCoinTest { ...@@ -157,6 +161,7 @@ public class ServicioUjaCoinTest {
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
public void testTransferencia() { public void testTransferencia() {
// Registrar cliente // Registrar cliente
Cliente cliente = new Cliente( Cliente cliente = new Cliente(
...@@ -212,6 +217,7 @@ public class ServicioUjaCoinTest { ...@@ -212,6 +217,7 @@ public class ServicioUjaCoinTest {
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
public void testReintegroDoble() { public void testReintegroDoble() {
// Registrar cliente // Registrar cliente
Cliente cliente = new Cliente( Cliente cliente = new Cliente(
...@@ -240,6 +246,7 @@ public class ServicioUjaCoinTest { ...@@ -240,6 +246,7 @@ public class ServicioUjaCoinTest {
} }
@Test @Test
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
public void testReintegroDobleParalelo() { public void testReintegroDobleParalelo() {
// Registrar cliente // Registrar cliente
Cliente cliente = new Cliente( Cliente cliente = new Cliente(
...@@ -287,9 +294,4 @@ public class ServicioUjaCoinTest { ...@@ -287,9 +294,4 @@ public class ServicioUjaCoinTest {
} }
}).isInstanceOfAny(SaldoInsuficienteParaOperacion.class); }).isInstanceOfAny(SaldoInsuficienteParaOperacion.class);
} }
@BeforeEach
void limpiarBaseDatos() {
limpiadorBaseDatos.limpiar();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment