'Not able to run Junit test with H2 database SpringBoot
I have a strange problem with my unit test.. My goal is to run test on a H2 database and not on my Mysql database..
Actually the strange thing is that when i click on maven test it runs the app, trying to connect to mysql.. crash and the start with the h2 database and unit test fail..
@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);
}
}
Properties
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
username: root
password:
jpa:
open-in-view: true
hibernate:
ddl-auto: create-drop
Running with the test profile activated it throws
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:
Dependencies
<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>
Solution 1:[1]
try to use this h2 file cofiguration :
@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;
}
}
and modify the application.properties like this :
Datasource configuration
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
add this dependency :
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
it work it for me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | invzbl3 |
