[英]Insert data in h2 database through data.sql file before performing unit testing in spring boot
I want to perform unit testing in spring boot+ JPA.我想在 spring boot+ JPA 中执行单元测试。 For that I created configuration file to create bean for dataSource, all hibernate properties, entityManagerFactory and transactionManager.为此,我创建了配置文件来为 dataSource、所有休眠属性、entityManagerFactory 和 transactionManager 创建 bean。 Everything going perfect.一切都很完美。 Tables are getting created by model classes.表是由模型类创建的。 but now I want to insert data in all tables of database for testing through data.sql file.但现在我想通过data.sql文件在数据库的所有表中插入数据进行测试。 I kept data.sql file in src/main/resources but it not loading the file.我将 data.sql 文件保存在 src/main/resources 中,但它没有加载文件。 So how can I load data in h2 database before starting with unit testing.那么如何在开始单元测试之前在 h2 数据库中加载数据。
This is my cofiguration file -这是我的配置文件 -
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.cfg.Environment;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableJpaRepositories(basePackages = "base_package_name")
@EnableTransactionManagement
public class JPAConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1");
/*dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:mem:testdb");*/
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "false");
return properties;
}
@Bean(name="entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
LocalContainerEntityManagerFactoryBean lcemfb
= new LocalContainerEntityManagerFactoryBean();
lcemfb.setDataSource(this.dataSource());
lcemfb.setPackagesToScan(new String[] {"Package_to_scan"});
HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
lcemfb.setJpaVendorAdapter(va);
lcemfb.setJpaProperties(this.hibernateProperties());
lcemfb.afterPropertiesSet();
return lcemfb;
}
@Bean
public PlatformTransactionManager transactionManager(){
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(
this.entityManagerFactoryBean().getObject() );
return tm;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
}
According to another question on StackOverflow, you can initialise a database by adding a @Configuration
class to your tests, as follows:根据 StackOverflow 上的另一个问题,您可以通过向测试添加@Configuration
类来初始化数据库,如下所示:
@Configuration
public class DatabaseTestConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:schema.sql")
.addScript("classpath:test-data.sql")
.build();
}
}
To note that the above doesn't use JPA so you may have to adjust it for your own purposes, but it should give a good hint on how you could do it in JPA as well.请注意,上述内容不使用 JPA,因此您可能必须根据自己的目的对其进行调整,但它也应该很好地提示您如何在 JPA 中进行调整。
My preference however leans towards using the @Sql
annotation in each test, to initialise the data and then to clean it up.然而,我倾向于在每个测试中使用@Sql
注释,初始化数据然后清理它。 While this means more repetition, which is usually bad, it helps ensuring that tests always run from a clean slate.虽然这意味着更多的重复,这通常是不好的,但它有助于确保测试总是从头开始。
References参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.