我的单元测试有个奇怪的问题。我的目标是在H2数据库上运行测试,而不是在Mysql数据库上运行。
实际上,奇怪的是,当我单击maven测试时,它运行应用程序,试图连接到mysql..崩溃和开始与h2数据库和单元测试失败。
@ExtendWith(SpringExtension.class)
@SpringBootTest
// @ActiveProfiles("test") // Without this it runs on my mysql and works.. ,
// with this annotation the behaviour is described above
@Transactional
class BelugaprojectsApplicationTests {
@Autowired
private ICheckConfigService iCheckConfigService;
@Autowired
private CheckConfigJpaRepository checkConfigJpaRepository;
@Test
void getAllCheckConfigDeploiement() {
assertThat(iCheckConfigService.getAllCheckConfigDeploiement(
Integer.parseInt(AppConstants.DEFAULT_PAGE_NUMBER),
Integer.parseInt(AppConstants.DEFAULT_PAGE_SIZE),
"id").getTotalElements() > 0);
}
}
属性
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
username: root
password:
jpa:
open-in-view: true
hibernate:
ddl-auto: create-drop
在启动测试配置文件时运行它抛出的
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "HISTORIQUEDEPLOIEMENT" non trouvée
Table "HISTORIQUEDEPLOIEMENT" not found; SQL statement:
alter table historiquedeploiement add constraint FK134pkswiisobg18okjr9pegt7 foreign key (id_namespace) references namespace (id) [42102-200]
Table "CHECKCONFIGDEPLOIEMENT" non trouvée
Table "CHECKCONFIGDEPLOIEMENT" not found; SQL statement:
依赖关系
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
发布于 2022-01-24 21:57:00
尝试使用此h2文件配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.example.repository")
public class DatabaseConfig {
@Autowired
private DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new
LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan(new String[] { "com.example.model" });
em.setPersistenceUnitName("entityManager");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "create");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
return properties;
}
}
然后像这样修改application.properties:
数据源配置
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:MyProjectName;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.application.admin.enabled=true
添加此依赖项:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
对我来说很管用
https://stackoverflow.com/questions/70840879
复制相似问题