Commit 2d303701 by Antonio Rueda

Cambio testing JPA usando base de datos para testing con drop-and-create

parent ea948d43
## Fichero de configuración para UjaCoin durante testing
spring.datasource.url: jdbc:mysql://localhost:33060/ujacoin_test
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 */
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