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.