我正在尝试为持久性课程正确设置junit测试,即使我得到'绿色条',我也担心几件事情。 当我在调试器中运行测试文件时,我可以看到testList()所有对象都按预期加载,如果有任何一个有子项,它们也会被加载。
For testList(), after I do...
instance.update(sg);
List <SettingsGroup> sl = instance.list();
...我在调试器(最后的截图)中看到没有加载子项。
更新元素并将其检索回以验证更新确实发生的正确方法是什么? 现在我正在做类似
的事情instance.update(sg);
List <SettingsGroup> sl = instance.list();
......但我不确定。
当我尝试testDelete()时,elments列表的大小与删除前的大小相同,这让我感到奇怪。
当试图研究hibernate测试时,我遇到了很多简单的例子,但我还没有得到如何正确设置,拆除和任何会话/事务管理。
我对应这个类的表有5个'硬编码'行,所以我很确定'读'调用。
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 );
}
}
上面的测试配置:
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;}
}
答案 0 :(得分:0)
一些想法......你所做的实际上是一个集成测试,因为它需要一个数据库。其次,不是肯定的,但是在成功查询更新的行之前,是否必须提交更新?