我想为我的项目创建一个集成测试,并且我是 Spring Test DB Unit 的新手。不幸的是,在运行测试时,我遇到了以下错误:
我还添加了 Base Test Config Class 文件,其中配置以及如何连接到 H2 数据库。
//import classes
@Configuration
@ComponentScan( {"com.projects.person", "com.projects.commons"} )
@EnableWebMvc
@EnableTransactionManagement
@PropertySource( "classpath:application.properties" )
public class BaseTestConfig extends WebMvcConfigurerAdapter
{
@Autowired
private Environment env;
@Autowired
private OperatingSystem operatingSystem;
@Autowired
private PropertiesFactory propertiesFactory;
@Bean
public UrlBasedViewResolver setupViewResolver()
{
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix( "/WEB-INF/pages/" );
resolver.setSuffix( ".jsp" );
resolver.setViewClass( JstlView.class );
return resolver;
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan( "com.projects.person.model.db" );
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
String databaseUrl = "jdbc:h2:tcp://localhost/persondb";
if( operatingSystem.isWindows() )
{
databaseUrl = "jdbc:h2:tcp://localhost/~/persondb";
}
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl( databaseUrl );
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
@Bean
public H2Connection dbUnitConnection() throws SQLException, DatabaseUnitException {
return new H2Connection(dataSource().getConnection(), "Person");
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.show_sql", "false");
return properties;
}
@Bean
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager transactionManager = propertiesFactory.createTransactionManagerProperty();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
}