我收到此错误:
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
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 mh.scene.terrain.MapReaderTestNew.testAcceptValidFilenames(MapReaderTestNew.java:26)
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.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
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:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
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:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
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 mh.scene.terrain.TMXMapReader.<clinit>(TMXMapReader.java:84)
... 36 more
这是一个众所周知的问题。问题是我无法找到一个非常好的解决方案。测试代码如下:
package mh.scene.terrain;
import static org.easymock.EasyMock.createMock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import mh.CachedResources;
import mh.scene.entity.player.EntityPlayerLocal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@SuppressWarnings("javadoc")
@RunWith(PowerMockRunner.class)
//@PrepareForTest(CachedResources.class)
public class MapReaderTestNew {
@Test
public void testAcceptValidFilenames() {
// Arrange
TMXMapReader reader = new TMXMapReader();
// Assert
assertTrue(reader.accept(new File("example.tmx")));
assertTrue(reader.accept(new File("/tmp/example.tmx")));
assertTrue(reader.accept(new File("/tmp/example.tsx")));
assertTrue(reader.accept(new File("/tmp/example.tmx.gz")));
assertFalse(reader.accept(new File("/tmp/example.txt")));
assertFalse(reader.accept(new File("/tmp/example.xml")));
assertFalse(reader.accept(new File("/tmp/example.exe")));
assertFalse(reader.accept(new File("/tmp/example")));
}
@Test
public void testReadingMapExample() throws Exception {
EntityPlayerLocal pl = createMock(EntityPlayerLocal.class);
Map map = new TMXMapReader().readMap("/sewers.tmx", null, pl);
// PowerMock.mockStatic(CachedResources.class);
// PowerMock.replay(CachedResources.class);
assertEquals(50, map.getHeight());
assertEquals(50, map.getWidth());
assertEquals(1, map.getTilesetCount());
assertEquals(3, map.getLayerCount());
assertEquals(1, map.getObjectsetCount());
}
}
这非常简单,甚至删除了&#34; PowerMock.mockStatic()&#34;添加RunWith
时出现错误任何想法?
问题似乎出现在这部分代码中:
File JAR_FILE = (MagicHogwarts.class.getProtectionDomain().getCodeSource().getLocation() == null ? null : new File(MagicHogwarts.class.getProtectionDomain().getCodeSource().getLocation().getPath()));
返回null。处理可通过powermock测试的路径的正确方法是什么?只有当我使用语句&#34; RunWith&#34;
时才会抛出该特定错误答案 0 :(得分:1)
尝试添加此注释
@SuppressStaticInitializationFor( {"package.YourClass"})
为了模拟静态,你需要它为powermockito知道它需要抑制静态作为更改字节码的一部分
答案 1 :(得分:0)
您可能还需要将EntityPlayerLocal.class
添加到@PrepareForTest()
。
我猜测createMock(EntityPlayerLocal.class)
方法导致了这个问题。
你能检查一次写入的代码吗?如果您也可以在您的问题中粘贴该方法,那会更好。