cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

LogUtilities and JUnit tests

rtaylor-3
12-Amethyst

LogUtilities and JUnit tests

Greetings,

I'm trying to write a simple unit test for  java ThingShape.

My code has a static initializer for the logger

Line 42: private static Logger logger = LogUtilities.getInstance().getApplicationLogger(PredictableAsset.class);

When i run the corresponding test class i'm getting an error:

java.lang.ExceptionInInitializerError

  at java.lang.Class.forName0(Native Method)

  at java.lang.Class.forName(Class.java:264)

  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 com.ptc.cslm.pa.common.entity.thingshape.PredictableAssetTest.setup(PredictableAssetTest.java:38)

  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  at java.lang.reflect.Method.invoke(Method.java:497)

  at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:133)

  at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)

  at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)

  at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)

  at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)

  at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)

  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)

  at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)

  at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)

  at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)

  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: java.lang.NullPointerException

  at com.ptc.cslm.pa.common.entity.thingshape.PredictableAsset.<clinit>(PredictableAsset.java:42)

  ... 34 more

Has anyone else run into this. There is probably some magical annotation i need to add or worse, i have to mock the LogUtilities.

Any advice appreciated.

ACCEPTED SOLUTION

Accepted Solutions

Ok...was working until the logger was actually invoked. Was getting a NPE.

Had to use the specific implementation of Logger for this to work.

@RunWith(PowerMockRunner.class)

@PrepareForTest({LogUtilities.class, EntityUtilities.class,ch.qos.logback.classic.Logger.class})

public class PredictableAssetTest {

PowerMock.mockStatic(LogUtilities.class);

  LogUtilities lu = PowerMock.createMock(LogUtilities.class);

  ch.qos.logback.classic.Logger logger = PowerMock.createMock(ch.qos.logback.classic.Logger.class);

  EasyMock.expect(LogUtilities.getInstance()).andReturn(lu);

  EasyMock.expect(lu.getApplicationLogger(PredictableAsset.class)).andReturn(logger);

  PowerMock.replay(LogUtilities.class, lu);

View solution in original post

2 REPLIES 2

Ugggh....Have to mock LogUtilities.

  PowerMock.mockStatic(LogUtilities.class);

  LogUtilities lu = PowerMock.createMock(LogUtilities.class);

  EasyMock.expect(LogUtilities.getInstance()).andReturn(lu);

Then it works

Ok...was working until the logger was actually invoked. Was getting a NPE.

Had to use the specific implementation of Logger for this to work.

@RunWith(PowerMockRunner.class)

@PrepareForTest({LogUtilities.class, EntityUtilities.class,ch.qos.logback.classic.Logger.class})

public class PredictableAssetTest {

PowerMock.mockStatic(LogUtilities.class);

  LogUtilities lu = PowerMock.createMock(LogUtilities.class);

  ch.qos.logback.classic.Logger logger = PowerMock.createMock(ch.qos.logback.classic.Logger.class);

  EasyMock.expect(LogUtilities.getInstance()).andReturn(lu);

  EasyMock.expect(lu.getApplicationLogger(PredictableAsset.class)).andReturn(logger);

  PowerMock.replay(LogUtilities.class, lu);

Announcements


Top Tags