We are using DB Unit to test some of our classes. Our main database is Oracle but for DB Unit we work with HSQLDB.
We have a class that has two attributes like these:
@Column(columnDefinition="NUMBER(6,2)")
private Double height;
@Column(columnDefinition="NUMBER(6,2)")
private Double weight;
This is our test context configuration class:
@Configuration
@EnableJpaRepositories(basePackages = {"com.mycompany"})
@ComponentScan(basePackages = "com.mycompany")
public class TestContextConfiguration {
private final String DB_DRIVER = "org.hsqldb.jdbcDriver";
private final String DB_URL = "jdbc:hsqldb:mem:test;sql.syntax_ora=true";
private final String DB_USER = "sa";
private final String DB_PASS = "";
private final String PACKAGES = "com.mycompany.myapp.domain";
private final String PERSISTENCE_UNIT_NAME = "testDatabase";
private final String PERSISTENCE_UNIT_LOCATION = "classpath:META-INF/persistence.xml";
@Bean
public DataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(DB_DRIVER);
ds.setUrl(DB_URL);
ds.setUsername(DB_USER);
ds.setPassword(DB_PASS);
return ds;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setJpaDialect(new HibernateJpaDialect());
em.setPackagesToScan(PACKAGES);
em.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
em.setPersistenceXmlLocation(PERSISTENCE_UNIT_LOCATION);
em.setJpaProperties(jpaProperties());
em.afterPropertiesSet();
return em.getObject();
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(entityManagerFactory());
return tm;
}
public Properties jpaProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
return properties;
}
}
And this is one of the tests:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContextConfiguration.class})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public class GestionUsuarioServiceDbunitTest {
@Autowired
private UserServiceImpl userService;
@Test
@DatabaseSetup("/META-INF/dataset-test.xml")
public void testFindByLogin() throws Exception {
User user = userService.findByLogin("admin");
assertEquals("Admin", user.getNombre());
}
}
When we run it we get the following error:
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: NUMBER
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ParserDQL.readTypeDefinition(Unknown Source)
at org.hsqldb.ParserDDL.readColumnDefinitionOrNull(Unknown Source)
at org.hsqldb.ParserDDL.compileCreateTableBody(Unknown Source)
at org.hsqldb.ParserDDL.compileCreateTable(Unknown Source)
at org.hsqldb.ParserDDL.compileCreate(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 110 more
Note that I already told the DB sql.syntax_ora=true
What's happening here?