我正在尝试为持久性类正确设置junit测试,即使我得到“绿色条”,我也担心几件事。当我在调试器中运行测试文件时,我可以看到 testList() 所有对象都按预期加载,如果任何一个有孩子,它们也会被加载。
For testList(), after I do...
instance.update(sg);
List <SettingsGroup> sl = instance.list();
...我在调试器(最后的屏幕截图)中看到未加载子级。
更新元素并将其取回以验证更新是否发生的正确方法是什么?现在我正在做类似的事情
instance.update(sg);
List <SettingsGroup> sl = instance.list();
...但我不确定。
当我尝试 testDelete() 时,元素列表的大小与删除之前相同,这让我感到奇怪。
当试图研究休眠测试时,我遇到了很多简单的例子,但我还没有得到如何正确设置、拆除和任何会话/事务管理。
我与这个类对应的表有 5 个“硬编码”行,所以我很确定“读取”调用。
测试班
@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 );
}
}
为以上测试配置:
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();
}
}
域类:
@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;}
}