懒得获取数据 - JUnit测试

时间:2013-12-02 10:37:14

标签: java hibernate unit-testing

我想将Junit测试添加到客户已经使用的代码中。 该测试将涵盖DAO类。这个Junit Test使用hibernate,Spring和hlsql作为虚拟数据库。

这是我得到的错误:

  

错误[main] LazyInitializationException。(19)|没懒得   初始化角色集合:   com.projet.dao.model.scenario.CategoryEntity.resultDescList,no   会议或会议已经结束 -

我已经在Google上广泛了解此问题。我发现通过添加@OneToMany(fetch = FetchType.EAGER)  对于对象resultDescList的实体“CategoryEntity”,它修复了该问题。但是这个解决方案是不可接受的,因为我不应该修改代码来修复junit。

我还在测试中添加了@Transactional注释。但错误仍然存​​在。我有什么其他选择来解决这个问题?

单元测试:

@Test
@Transactional
    public final void testGetCategoryById() throws Exception
    {
        final Integer id = 1;
        CategoryEntity category = categoryDAO.getCategoryById(id);
        Assert.assertNotNull(category);
        Assert.assertEquals(id, category.getId());
        Assert.assertEquals("junit.cat.test1", category.getKey());
        Assert.assertEquals(1, category.getOrder().intValue());

        List< ResultDescEntity > resultDescList = category.getResultDescList();
        Assert.assertTrue(resultDescList.size() == 1);

        UnitTestEntity unitTest = category.getUnitTest();
        Assert.assertNotNull(unitTest);
        Assert.assertEquals(1, unitTest.getId().intValue());
        Assert.assertEquals(true, category.getVisible());
    }

CategoryEntity类的摘录:

Entity
@Table(name = "CATEGORY")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@org.hibernate.annotations.Entity(selectBeforeUpdate = false, dynamicInsert = false, dynamicUpdate = false, optimisticLock = OptimisticLockType.NONE)
public class CategoryEntity implements IGenericEntity
{
    @Id
    @Column(name = "CATEGORY_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CategorySequence")
    @javax.persistence.SequenceGenerator(name = "CategorySequence", allocationSize = 1, sequenceName = "category_seq")
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "UNITTEST_ID")
    private UnitTestEntity unitTest;

    @Column(name = "KEY", length = 50)
    private String key;

    @Column(name = "CATEGORY_ORDER")
    private Integer order;

    @Column(name = "VISIBLE")
    private Integer visible;

    @OneToMany(mappedBy = "category", cascade = { CascadeType.ALL })
    @IndexColumn(name = "RESULT_ORDER", base = 1)
    private List<ResultDescEntity> resultDescList;

此致

0 个答案:

没有答案