使用 PowerMock 时出现 ExceptionInInitializerError
ExceptionInInitializerError when using PowerMock
我有一个测试 class 如下所示。需要在 HmUtils.class,
里面模拟一个静态方法
@RunWith(PowerMockRunner.class)
@PrepareForTest({Environment.class, HmUtils.class})
public class MyUtilTest {
@Mock
Context mockedContext;
@Before
public void initialSetup()
{
System.out.println("initSetup Executed:");
mockedContext = PowerMockito.mock(Context.class);
PowerMockito.mockStatic(Environment.class);
PowerMockito.mockStatic(HmUtils.class);
}
@Test
public void DeviceTest() throws Exception
{
System.out.println("DeviceTest Executed:");
when(Environment.getExternalStorageDirectory()).thenReturn(new File("testFile"));
when(Environment.getExternalStorageDirectory()
.getAbsolutePath()).thenReturn(anyString());
HmUtils.setCurrentBTAddress(null);
}
在 HmUtils.class 中,我有一个这样的静态值(第 332 行)
public static final String TEST_FOLDER = Environment.getExternalStorageDirectory()
.getAbsolutePath();
这会抛出一个错误,例如 "Environment" getmethod is not mocked。所以我模拟了 Environment class 并尝试 return getExternalStorageDirectory() 和 getAbsolutePath() 的值,如上所述。但它仍然显示错误
java.lang.ExceptionInInitializerError
at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
Caused by: java.lang.NullPointerException
at com.package.android.app.mymanager.util.HmUtils.<clinit>(HmUtils.java:332)
在LogUtils.class中,我在这一行出错
public class LogUtils
{
private static final String TEST_FILE_FOLDER = Environment.getExternalStorageDirectory()
.getAbsolutePath();
}
在LogUtilsTest.Class中,我通过下面的代码片段解决了环境异常初始化错误
@RunWith(PowerMockRunner.class)
@PrepareForTest({Environment.class})
public class LogUtilsTest {
private File file;
@Before
public void initialSetup() {
PowerMockito.mockStatic(Environment.class);
file = mock(File.class);
when(Environment.getExternalStorageDirectory()).thenReturn(file);
when(file.getAbsolutePath()).thenReturn("abc");
( OR )
//when(file.getAbsolutePath()).thenReturn(Mockito.anyString());
}
@Test
public void log_d() {
LogUtils.log_d("tag", "message");
}
}
我有一个测试 class 如下所示。需要在 HmUtils.class,
里面模拟一个静态方法@RunWith(PowerMockRunner.class)
@PrepareForTest({Environment.class, HmUtils.class})
public class MyUtilTest {
@Mock
Context mockedContext;
@Before
public void initialSetup()
{
System.out.println("initSetup Executed:");
mockedContext = PowerMockito.mock(Context.class);
PowerMockito.mockStatic(Environment.class);
PowerMockito.mockStatic(HmUtils.class);
}
@Test
public void DeviceTest() throws Exception
{
System.out.println("DeviceTest Executed:");
when(Environment.getExternalStorageDirectory()).thenReturn(new File("testFile"));
when(Environment.getExternalStorageDirectory()
.getAbsolutePath()).thenReturn(anyString());
HmUtils.setCurrentBTAddress(null);
}
在 HmUtils.class 中,我有一个这样的静态值(第 332 行)
public static final String TEST_FOLDER = Environment.getExternalStorageDirectory()
.getAbsolutePath();
这会抛出一个错误,例如 "Environment" getmethod is not mocked。所以我模拟了 Environment class 并尝试 return getExternalStorageDirectory() 和 getAbsolutePath() 的值,如上所述。但它仍然显示错误
java.lang.ExceptionInInitializerError
at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
Caused by: java.lang.NullPointerException
at com.package.android.app.mymanager.util.HmUtils.<clinit>(HmUtils.java:332)
在LogUtils.class中,我在这一行出错
public class LogUtils
{
private static final String TEST_FILE_FOLDER = Environment.getExternalStorageDirectory()
.getAbsolutePath();
}
在LogUtilsTest.Class中,我通过下面的代码片段解决了环境异常初始化错误
@RunWith(PowerMockRunner.class)
@PrepareForTest({Environment.class})
public class LogUtilsTest {
private File file;
@Before
public void initialSetup() {
PowerMockito.mockStatic(Environment.class);
file = mock(File.class);
when(Environment.getExternalStorageDirectory()).thenReturn(file);
when(file.getAbsolutePath()).thenReturn("abc");
( OR )
//when(file.getAbsolutePath()).thenReturn(Mockito.anyString());
}
@Test
public void log_d() {
LogUtils.log_d("tag", "message");
}
}