我写了使用多个数据仓库JPA测试Service
。问题是,一些仓库使用大量的原生查询与MySQL
特定功能,如str_to_date()
。所以,当我尝试使用H2
服务的方法来测试我得到一个错误,指出H2不能识别功能。我曾在MySQL模式下使用H2尝试过,但得到了同样的错误。
here mariaDB4j提议作为一个变通。我已经加入到依赖的Maven
<dependency>
<groupId>ch.vorburger.mariaDB4j</groupId>
<artifactId>mariaDB4j</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
但越来越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
。
我的测试文件看起来是这样的:
@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*/
}
该项目是建立与注释驱动春季启动。
这听起来像你需要显式声明的DataSource
做检查。在H 2的情况下,有可能已经通过弹簧测试依赖性声明一个数据源豆,但有可能无法通过ch.vorburger.mariaDB4j
提供关断的,现成的一个。
下面是一个嵌入式MariaDB的数据源,我从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();
}
}
我建立了下面的类,我在每一个集成测试,需要到mariadb
数据库访问重用。这也许可以改善(和我很高兴建议),但它的作品至今:
@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=