Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:57:28+00:00 2026-06-04T05:57:28+00:00

I am trying to write a unit test for the following class: @Transactional public

  • 0

I am trying to write a unit test for the following class:

@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;
}
}

There are some other methods, but this setup code is what’s relevant to my question. In my unit test, I want to mock (using EasyMock) the SessionFactory object sf, as well as the Session and Transaction objects it will return:

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
}

When I try to run this with JUnit, I get the following error:

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)

There is no public INSTANCE variable on sf that I can see. I’m new to EasyMock, so I think my question is: what should I tell my mock to return for INSTANCE, and how do I tell it to do that? I don’t know why the SessionFactory setup code is being called by setSf() at all, so that’s another mystery. The more general question is: how should I mock up a SessionFactory object anyway?

Update

I figured out that catching the exception without doing anything will work just fine. I have changed my EditorHelper class as follows:

@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();
        }
    }
...

Once this static block completes, I can set the SessionFactory member to point to my mock object and everything works from there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:57:30+00:00Added an answer on June 4, 2026 at 5:57 am

    The code is being run because it is inside a static initialization block. Once your test refers to EditorHelper, the JVM will load the class and this initialization block will run and attempt to create a SessionFactory from the AnnotationConfiguration instance which is configured with the config XML file.

    Which class are you trying to test? I note that the test is EditorTest and the class is EditorHelper. Is this intentional, and are you actually trying to test an Editor class?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a Unit test for a class that has several
I am new to spock. I am trying to write a spock unit test
I'm trying to write a unit test for a class that generates distinct strings.
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
I am trying to write a unit test for the following method in my
I have the following situation. I am trying to write a unit test for
I am trying to write a unit test for an action method which calls
I'm trying to write a unit test that will loop through all action methods
i'm trying to write an unit test for my Android's RelativeLayout. Currently, my testcode
I am trying to write a unit test for my one controller to verify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.