Unit test for Spring DAO-based Hibernate JPA downloads

I am trying to write unit tests for my Spring Boot application that uses Hibernate / JPA and DAO objects. The following are the Ive steps that have been completed so far:

1) Added the 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 the file .. /test/resources/application.properties Ive added the following:

 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 Ive added some "paste into ..." data creation scripts.

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

4) unit test is as follows:

 @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, for example:

 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 person:

 @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?

+7
spring spring-boot unit-testing hibernate jpa
source share
1 answer

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

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

This part is described here 74.3 Initializing a Database Using Spring JDBC :

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

+1
source share

All Articles