Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

5) Group Entity:

@Entity
@javax.persistence.Table(name = "groups", uniqueConstraints = {
        @UniqueConstraint(columnNames = "GROUP_NAME"),
})
public class Group {

    // ==============
    // PRIVATE FIELDS
    // ==============

    // An autogenerated id (unique for each group in the db)
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "GROUP_ID", unique = true, nullable = false)
    private long id;

    @Column(name = "GROUP_NAME", unique = true, nullable = false)
    private String name;

What am I missing?

like image 950
DilTeam Avatar asked Apr 29 '15 15:04

DilTeam


1 Answers

DilTeam, I found that your last comment was very important!

Spring does not allow to use schema.sql (without platform siffix) with ddl-auto=create-drop propertie value.

This part is described here 74.3 Initialize a database using Spring JDBC:

If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. To avoid those errors set ddl-auto explicitly to "" (preferable) or "none". Whether or not you use ddl-auto=create-drop you can always use data.sql to initialize new data.

like image 118
Konstantin Konyshev Avatar answered Oct 11 '22 21:10

Konstantin Konyshev