I’m trying to properly set up junit test for a persistance class and even though I’m getting ‘green bar’, I’m worried about couple of things.
When i run the test file in debugger, I can see for the testList() that all object are loaded as expected and if any one has children, they are loaded as well.
For testList(), after I do...
instance.update(sg);
List <SettingsGroup> sl = instance.list();
… I see in debugger (screenshot at the end) that children are not loaded.
What’s the proper way to update an element and retrieve it back to verify update did happen?
Right now I’m doing something like
instance.update(sg);
List <SettingsGroup> sl = instance.list();
… but I’m not sure about that.
When I’m trying to testDelete(), size of list of elments is the same as before delete and it makes me wonder.
When tried to research hibernate testing, I’m running into a lot of simple examples, but I’m just not getting yet how to do proper set up, tear down and any session/transaction management.
I table corresponding to this class has 5 ‘hardcoded’ rows, so I pretty sure about ‘read’ calls.
TEST CLASS
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = {"applicationContext.xml"})
@Transactional
public class SettingGroupDaoTest extends AbstractJUnit4SpringContextTests {
public SettingGroupDaoTest(){}
SettingGroupDao instance;
@BeforeClass
public static void setUpClass() throws Exception {}
@AfterClass
public static void tearDownClass() throws Exception {}
@Before
public void setUp() {
Configuration c = HibernateConfiguration.getHibernateConfiguration()
.configuration(SettingsGroup.class);
SessionFactory sessionFactory = c.buildSessionFactory();
instance = new SettingGroupDao();
instance.setSessionFactory( sessionFactory );
}
@Test
public void testList() throws Exception {
SettingsGroup sq = instance.get(1);
SettingsGroup s1 = sq.getChildren().get(0);
System.out.println( "children index 2 name: " + s1.getName() );
System.out.println( "2nd element of children: " + sq.getChildren().get(1).getName() );
}
@Test
public void testUpdate() throws Exception{
SettingsGroup sg = instance.get(1);
sg.setName("Updated Name of Setting Group");
instance.update(sg);
List <SettingsGroup> sl = instance.list();
Hibernate.initialize( instance.list() );
System.out.println( "UPDATED Settings Group Name: " + sg.getName() );
}
@Test
public void testDelete() throws Exception{
instance.delete( 0 );
int s = instance.list().size();
System.out.println( "Size after delete: " + s );
}
}
CONFIG FOR TEST ABOVE:
public class HibernateConfiguration {
private HibernateConfiguration(){}
public Configuration configuration( java.lang.Class clazz ) {
Configuration c = new Configuration();
c.setProperty(Environment.DRIVER, "org.postgresql.Driver");
c.setProperty(Environment.URL, "jdbc:postgresql://localhost/###");
c.setProperty(Environment.USER, "postgres");
c.setProperty(Environment.PASS, "####");
c.setProperty(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
c.setProperty(Environment.AUTOCOMMIT, "true"); //"true");
c.addClass( clazz );
return c;
}
public static HibernateConfiguration getHibernateConfiguration(){
return new HibernateConfiguration();
}
}
DOMAIN CLASS:
@Entity
@SequenceGenerator(name = "settings_group_id_seq", sequenceName = "settings_group_id_seq")
@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name="settings_group")
public class SettingsGroup implements Serializable, DomainObject {
private int id;
private String name;
private String code;
private List<SettingsGroup> children = new LinkedList<SettingsGroup>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "settings_group_id_seq")
@Column(name = "id", insertable = false)
public int getId() {return id;}
public void setId(int id) {this.id = id;}
@Column(name = "name")
public String getName() {return name;}
public void setName(String name) {this.name = name;}
@Column( name = "code")
public String getCode() {return code; }
public void setCode(String code) {this.code = code;}
@OneToMany( fetch = FetchType.EAGER)
@JoinColumn( name = "parent_id")
@OrderColumn
public List<SettingsGroup> getChildren() {return children;}
public void setChildren(List<SettingsGroup> children) {this.children = children;}
}

A couple of thoughts … what you’re doing is really an integration test since it requires a database. Secondly, not positive, but don’t you have to commit the update before you can successfully query the updated rows?