Estoy escribiendo pruebas de Service
que utiliza varios repositorios de datos Jpa. El problema es que algunos repositorios utilizan una gran cantidad de consultas nativas con MySQL
funciones específicas, tales como str_to_date()
. Así que cuando traté de probar el método del servicio utilizando H2
Tengo un error que dice que el H2 no reconoce la función. He intentado usar H2 en el modo de MySQL, pero tiene el mismo error.
aquí mariaDB4j fue propuesto como una solución alternativa. He añadido la dependencia en Maven
<dependency>
<groupId>ch.vorburger.mariaDB4j</groupId>
<artifactId>mariaDB4j</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
Pero conseguir IllegalStateException : Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase
.
Mi archivo de prueba se ve de esta manera:
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
public class TestPay {
@TestConfiguration
static class PaymentServiceTestContextConfiguration {
@Bean
public PaymentService paymentService(){
return new PaymentService();
}
}
@Autowired
private PaymentService paymentService;
@Autowired
private TarifRepository firstRepository;
@Autowired
private BuildingRepository secondRepository;
@Autowired
private ApartmentRepository thirdRepository;
/* Test cases here*/
}
El proyecto es impulsado por la acumulación de anotación con la primavera de arranque.
Construyo la siguiente clase que su reutilización en todas las pruebas de integración que requiere el acceso a la base de datos mariadb
. Probablemente se podría mejorar (y yo estaría feliz de sugerencias), pero funciona hasta el momento:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations="classpath:application-junit.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) //otherwise mariadb is not cleaned up between tests
public abstract class MyIntegrationTest {
private static MariaDB4jSpringService DB;
@BeforeClass
public static void init() throws ManagedProcessException {
DB = new MariaDB4jSpringService();
DB.setDefaultPort(1234);
DB.start();
DB.getDB().createDB("yourtables");
DB.getDB().source("schema.sql"); // init scripts from /src/test/resources/schema.sql
}
@AfterClass
public static void cleanup() {
if (DB != null) DB.stop();
}
}
application-junit.properties:
spring.datasource.url=jdbc:mariadb://localhost:1234/yourtables
spring.datasource.username=root
spring.datasource.password=