ExceptionInInitializerError when mocking org.hibernate.Hibernate.class

2,106 views
Skip to first unread message

Vanga

unread,
Apr 15, 2009, 10:14:04 AM4/15/09
to PowerMock
here is my code with imports

import static org.easymock.EasyMock.anyObject;
import static org.powermock.api.easymock.PowerMock.replay;

import org.hibernate.Hibernate;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit3.PowerMockSuite;
import junit.framework.TestCase;
import junit.framework.TestSuite;

@PrepareForTest(Hibernate.class)
public class TempTest extends TestCase {

@SuppressWarnings("unchecked")
public static TestSuite suite() throws Exception {
return new PowerMockSuite("Unit tests for " +
TempTest.class.getSimpleName(), TempTest.class);
}

public void testTemp(){
PowerMock.mockStatic(Hibernate.class);
Hibernate.initialize(anyObject());
replay(Hibernate.class);
}
}

Line PowerMock.mockStatic(Hibernate.class); causes
java.lang.ExceptionInInitializerError . Why? I need to mock
Hibernate.initialize(); . Who can help?

Johan Haleby

unread,
Apr 15, 2009, 10:46:13 AM4/15/09
to powe...@googlegroups.com
Could you please post the stack trance? Maybe you need to suppress a static initializer.

/Johan

Илья Иля

unread,
Apr 15, 2009, 11:00:01 AM4/15/09
to powe...@googlegroups.com
Here is stack trace:

java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:164)
    at javassist.runtime.Desc.getClassObject(Desc.java:43)
    at javassist.runtime.Desc.getClassType(Desc.java:152)
    at javassist.runtime.Desc.getType(Desc.java:122)
    at javassist.runtime.Desc.getType(Desc.java:78)
    at org.hibernate.Hibernate.<clinit>(Hibernate.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at net.sf.cglib.proxy.Enhancer.setCallbacksHelper(Enhancer.java:586)
    at net.sf.cglib.proxy.Enhancer.setThreadCallbacks(Enhancer.java:579)
    at net.sf.cglib.proxy.Enhancer.registerCallbacks(Enhancer.java:561)
    at org.powermock.api.easymock.internal.signedsupport.SignedSupportingClassProxyFactory.createProxy(SignedSupportingClassProxyFactory.java:156)
    at org.easymock.internal.MocksControl.createMock(MocksControl.java:40)
    at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2089)
    at org.powermock.api.easymock.PowerMock.mockStatic(PowerMock.java:290)
    at com.ibm.metro.ejb.rpa.TempTest.testTemp(TempTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.run(JUnit3TestSuiteChunkerImpl.java:157)
    at org.powermock.modules.junit3.PowerMockSuite.run(PowerMockSuite.java:52)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.commons.logging.impl.Jdk14Logger does not implement Log
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:532)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:272)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:414)
    at org.hibernate.type.NullableType.<clinit>(NullableType.java:35)
    ... 39 more
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: Class org.apache.commons.logging.impl.Jdk14Logger does not implement Log
    at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:416)
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:525)
    ... 42 more
Caused by: org.apache.commons.logging.LogConfigurationException: Class org.apache.commons.logging.impl.Jdk14Logger does not implement Log
    at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:412)
    ... 43 more

There is something with log4j. Maybe I need to disable it? but how, I can't change code.


2009/4/15 Johan Haleby <johan....@gmail.com>



--
Offering hope to life...

Jan Kronquist

unread,
Apr 15, 2009, 3:22:22 PM4/15/09
to powe...@googlegroups.com
Try to add
@PowerMockIgnore("org.apache.commons.logging")

/Jan

Илья Иля

unread,
Apr 15, 2009, 4:06:56 PM4/15/09
to powe...@googlegroups.com
Ok, I'll try tomorrow and say

15 апреля 2009 г. 22:22 пользователь Jan Kronquist <jan.kr...@gmail.com> написал:

Илья Иля

unread,
Apr 16, 2009, 11:30:07 AM4/16/09
to powe...@googlegroups.com
Hm, it works. But it works, even if I don't mock HIbernate.initialize() at all!! What does this annotation do?

2009/4/15 Илья Иля <lan...@googlemail.com>

Илья Иля

unread,
Apr 16, 2009, 11:46:26 AM4/16/09
to powe...@googlegroups.com
Sorry ) only one Hibernate.initialize() go on success. But others no. So I mock it and it works well now. What about this annotation is?

2009/4/16 Илья Иля <lan...@googlemail.com>

Jan Kronquist

unread,
Apr 25, 2009, 3:57:43 AM4/25/09
to powe...@googlegroups.com
This annotation tells PowerMock not to load classes starting with this
name with PowerMock's classloader. Instead the normal classloader will
be used:

http://powermock.googlecode.com/svn/docs/powermock-1.2/apidocs/org/powermock/core/classloader/annotations/PowerMockIgnore.html

/Jan

Илья Иля

unread,
Apr 26, 2009, 8:08:46 AM4/26/09
to powe...@googlegroups.com
great thanx!

25 апреля 2009 г. 10:57 пользователь Jan Kronquist <jan.kr...@gmail.com> написал:
Reply all
Reply to author
Forward
0 new messages