I am writing tests for Service that uses several data Jpa repositories. The problem is that some repositories use a lot of native queries with MySQL specific functions such as str_to_date(). So when I tried to test the service's method using H2 I got an error saying that H2 doesn't recognize function. I have tried using H2 in MySQL mode, but got the same error.
here mariaDB4j was proposed as a work-around. I have added dependency into Maven
<dependency>
<groupId>ch.vorburger.mariaDB4j</groupId>
<artifactId>mariaDB4j</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
But getting 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.
My Test file looks this way:
@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*/
}
The project is build with Annotation driven Spring Boot.
I build the following class that I reuse in every integration test that requires database access to mariadb. It could probably be improved (and I'd be happy for suggestions), but it works so far:
@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=
It sounds like you need to explicitly declare your DataSource for tests. In the case of h2, there is likely already a datasource bean declared by a spring test dependency, but there may not be an off-the-shelf one provided by ch.vorburger.mariaDB4j.
Here's an example of an embedded MariaDB DataSource that I stole from elsewhere on the internet
import ch.vorburger.mariadb4j.DBConfigurationBuilder
import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import javax.sql.DataSource
@Configuration
@Profile(['local', 'integrationTest'])
class EmbeddedMariaDbConfig {
@Bean
MariaDB4jSpringService mariaDB4jSpringService() {
new MariaDB4jSpringService()
}
@Bean
DataSource dataSource(MariaDB4jSpringService mariaDB4jSpringService,
@Value('${app.mariaDB4j.databaseName}') String databaseName,
@Value('${spring.datasource.username}') String datasourceUsername,
@Value('${spring.datasource.password}') String datasourcePassword,
@Value('${spring.datasource.driver-class-name}') String datasourceDriver) {
//Create our database with default root user and no password
mariaDB4jSpringService.getDB().createDB(databaseName)
DBConfigurationBuilder config = mariaDB4jSpringService.getConfiguration()
DataSourceBuilder
.create()
.username(datasourceUsername)
.password(datasourcePassword)
.url(config.getURL(databaseName))
.driverClassName(datasourceDriver)
.build();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With