mercredi 29 avril 2015

Unit test for Spring boot Hibernate JPA based DAOs

I am trying to write unit tests for my Spring Boot based application that uses Hibernate/JPA entities & DAOs. Here are the steps I’ve followed so far:

1) Added following to pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>runtime</scope>
</dependency>

2) In ../test/resources/application.properties, I’ve added this:

spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.database = HSQL
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HSQLDialect
spring.datasource.driverClassName = org.hsqldb.jdbcDriver
spring.datasource.url: jdbc:hsqldb:mem:scratchdb
spring.datasource.username = sa
spring.datasource.password =

3) In ../test/resources/import.sql I’ve added a few ‘insert into…’, data creation scripts.

insert into groups(GROUP_NAME, THREAD_POOL_SIZE) values ("TEST GROUP 1", 5);

4) The unit test looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)

public class TestGroupDao {

    @Autowired
    GroupDao groupDao;

    @Test
    public void testFindByName() {

        Group group = groupDao.findByName("TEST GROUP 1");
        //assertThat(group.getPoolSize(), is(equalTo(5)));
    }
}

When I run this Test, I get error messages such as:

org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table..
org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: user lacks privilege or object not found: PUBLIC.GROUP

What am I missing?

Aucun commentaire:

Enregistrer un commentaire