使用PowerMock时出现ExceptionInInitializerError

时间:2017-10-06 07:02:36

标签: java android junit mockito powermockito

我有一个如下所示的Test类。需要在HmUtils.class中模拟静态方法,

import unittest

from queues import *


class TestCase(unittest.TestCase):


    # testing an empty Array

    def test_if_empty(self):  # we will test if the array is empty using is_empty
        q = QueueLinked(3)  # [none,none,none]
        self.assertTrue(q.is_empty())  # Should be True

    def test_if_full(self):
        q = QueueLinked(3)
        q.enqueue(4)
        q.enqueue(5)
        q.enqueue(8)
        self.assertTrue(q.is_empty())

if (__name__ == '__main__'):

在HmUtils.class中,我有一个像这样的静态值(在第332行)

    Ran 2 tests in 0.000s

FAILED (errors=1)
Launching unittests with arguments python -m unittest test_queues.TestCase in C:\Users\M\Documents\CSC 202\Labs\Lab3
Error
Traceback (most recent call last):
  File "C:\Users\M\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Users\M\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 605, in run
    testMethod()
  File "C:\Users\M\Documents\CSC 202\Labs\Lab3\test_queues.py", line 17, in test_if_full
    q.enqueue(4)
  File "C:\Users\M\Documents\CSC 202\Labs\Lab3\queues.py", line 63, in enqueue
    oldrear.set_data(self.rear)
AttributeError: 'NoneType' object has no attribute 'set_data'

这会引发错误,例如"环境" getmethod没有被嘲笑。所以我模拟了Environment类,并尝试返回getExternalStorageDirectory(),getAbsolutePath()的值,如上所示。但它仍显示错误

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

1 个答案:

答案 0 :(得分:0)

LogUtils.class 中,我在此行中收到错误

  public class LogUtils
 {
    private static final String TEST_FILE_FOLDER = Environment.getExternalStorageDirectory()
        .getAbsolutePath();
 }  

LogUtilsTest.Class 中,我通过以下片段解析环境exceptionininitializererror

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