spring - JPA EntityManager values are not persisted in database when tested using Junit -


i using hibernate 4 spring 3 , when try junit test, values not persisted in database

in dao implementation class

@transactional @repository public class projectdaoimpl extends genericdaoimpl<project>         implements projectdao {  public void create(project project) {         entitymanager.persist(project);         system.out.println("val  2  -- "+project.getprojectno());     }  @persistencecontext     public void setentitymanager(entitymanager entitymanager) {         this.entitymanager = entitymanager;     } 

and in junit test have

@transactionconfiguration @contextconfiguration({"classpath:applicationcontext.xml"}) @transactional @runwith(springjunit4classrunner.class)  public class projecttest {  @resource projectservice projectservice;     @test     public void createproject(){         project project = new project();         project.setprojectname("999---");         projectservice.create(project);     } 

i able see value statement in console though, record not saved in database.

system.out.println("val  2  -- "+project.getprojectno()); 

how can resolve issue?

by default spring test rollback transactions in unit test causing them not appear in database.

you can change default setting adding following annotation test class, cause transactions committed.

@transactionconfiguration(defaultrollback=false) @contextconfiguration({"classpath:applicationcontext.xml"}) @transactional @runwith(springjunit4classrunner.class)  public class projecttest {     //tests here } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -