3

次のクラスの単体テストを作成しようとしています。

@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を使用して)モックしたいSessionFactorysf思いSessionますTransaction

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すると、モックオブジェクトを指すようにメンバーを設定でき、そこからすべてが機能します。

4

1 に答える 1

2

コードは静的初期化ブロック内にあるため、実行されています。テストが参照されると、JVMはクラスをロードし、この初期化ブロックが実行され、構成XMLファイルで構成されたインスタンスからのEditorHelper,作成を試みます。SessionFactoryAnnotationConfiguration

どのクラスをテストしようとしていますか?テストはEditorTestであり、クラスはEditorHelperです。Editorこれは意図的なものであり、実際にクラスをテストしようとしていますか?

于 2012-05-22T08:01:18.217 に答える