使用EasyMock模拟Hibernate的SessionFactory

时间:2012-05-21 16:33:38

标签: java hibernate unit-testing junit easymock

我正在尝试为以下类编写单元测试:

@Transactional
public class EditorHelper {

private static SessionFactory sf;

static {        
    ClassPathResource hbr = new ClassPathResource("hibernate.cfg.xml");
    File hbCfg = null;

    try {
        hbCfg = hbr.getFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (hbCfg != null) {
        sf = new AnnotationConfiguration().configure(hbCfg).buildSessionFactory(); // <-- Stack trace points here
    }
}
public static void setSf(SessionFactory sf) {
    EditorHelper.sf = sf;
}
}

还有其他一些方法,但这个设置代码与我的问题有关。在我的单元测试中,我想模拟(使用EasyMock)SessionFactory对象sf,以及它将返回的SessionTransaction个对象:

public class EditorTest {

    private SessionFactory sf;
    private Session s;
    private Transaction tx;
    private Long id = 1L;
    private String idStr = id.toString();

    @Before
    public void setUp() {

        sf = EasyMock.createMock(SessionFactory.class);
        s = EasyMock.createMock(Session.class);
        tx = EasyMock.createMock(Transaction.class);

        EditorHelper.setSf(sf); // <-- Stack trace points here

        EasyMock.expect(sf.getCurrentSession()).andReturn(s);
        EasyMock.expect(s.beginTransaction()).andReturn(tx);
    }
// Tests go here
}

当我尝试使用JUnit运行时,我收到以下错误:

java.lang.NoSuchFieldError: INSTANCE
    at org.hibernate.type.BasicTypeRegistry.<init>(BasicTypeRegistry.java:94)
    at org.hibernate.type.TypeResolver.<init>(TypeResolver.java:59)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:250)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:302)
    at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:87)
    at com.mypkg.helper.EditorHelper.<clinit>(EditorHelper.java:38)
    at com.mypkg.model.EditorTest.setUp(EditorTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我可以看到INSTANCE上没有公开sf变量。我是EasyMock的新手,所以我认为我的问题是:我应该告诉我的模拟器返回INSTANCE,我该怎么告诉它呢?我不知道为什么SessionFactory设置代码完全被setSf()调用,所以这是另一个谜。更一般的问题是:我应该如何模拟SessionFactory对象?

更新

我发现在不做任何事情的情况下捕获异常将会正常工作。我更改了我的EditorHelper课程如下:

@Transactional
public class EditorHelper {

private static SessionFactory sf;

static {    

...

    if (hbCfg != null) {
        try { // New try block lets the initializaiton fail
            sf = new AnnotationConfiguration().configure(hbCfg).buildSessionFactory();
        } catch (NoSuchFieldError e) {
            e.printStackTrace();
        }
    }
...

一旦这个静态块完成,我可以将SessionFactory成员设置为指向我的模拟对象,一切都可以从那里开始。

1 个答案:

答案 0 :(得分:2)

正在运行代码,因为它位于静态初始化块中。一旦您的测试引用EditorHelper,,JVM将加载该类,并且此初始化块将运行并尝试从配置了config XML文件的SessionFactory实例创建AnnotationConfiguration。 / p>

你想测试哪一堂课?我注意到测试是EditorTest,类是EditorHelper。这是故意的,你是否真的试图测试Editor类?