Unit test for Spring boot Hibernate JPA based DAOs -
i trying write unit tests spring boot based application uses hibernate/jpa entities & daos. here steps i’ve followed far:
1) added following 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 few ‘insert into…’, data creation scripts.
insert groups(group_name, thread_pool_size) values ("test group 1", 5);
4) unit test looks 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 run test, 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 // ============== // autogenerated id (unique each group in 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 missing?
dilteam, found last comment important!
spring not allow use schema.sql (without platform siffix) ddl-auto=create-drop propertie value.
this part described here 74.3 initialize database using spring jdbc:
if want use schema.sql initialization in jpa app (with hibernate) ddl-auto=create-drop lead errors if hibernate tries create same tables. avoid errors set ddl-auto explicitly "" (preferable) or "none". whether or not use ddl-auto=create-drop can use data.sql initialize new data.
Comments
Post a Comment