使用Mockito和PowerMock时初始化异常错误

 强悍的梅子 发布于 2023-02-09 14:38

我第一次使用Mockito和PowerMock,我在以下行运行下面的代码时遇到错误:

MockitoAnnotations.initMocks(SearchTest.class);

错误是:

java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
    at org.mockito.exceptions.base.MockitoException.(MockitoException.java:19)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
    at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
    at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
    at org.mockito.internal.configuration.ClassPathLoader.(ClassPathLoader.java:59)
    at org.mockito.internal.configuration.GlobalConfiguration.createConfig(GlobalConfiguration.java:38)
    at org.mockito.internal.configuration.GlobalConfiguration.(GlobalConfiguration.java:32)
    at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:94) 
Caused by: java.lang.NullPointerException

测试类的代码是:

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.junit.Assert;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized.Parameters;
    import org.mockito.Matchers;
    import org.mockito.Mock;
    import org.mockito.Mockito;
    import org.mockito.MockitoAnnotations;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(Parameterized.class)
@PrepareForTest(InputStreamReader.class)

public class SearchTest {   
    private String preFile;
    private String expectedPreFile;
    private String postFile;
    private String expectedpostFile;

    @Parameters
    public static Collection data() {
        return Arrays.asList(new Object[][] {    
        { "test1" } });
    }

    @Mock
    private HttpClient mockHttpClient;
    private HttpPost mockHttpPost;
    private HttpResponse mockHttpResponse;
    private HttpEntity mockHttpEntity; 
    private InputStream mockInputStream;
    private InputStreamReader mockInputStreamReader;
    private BufferedReader mockBufferedReader;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        xstream = new XStream();
    }


    public SearchTest(String folder) {
        this.preFile= folder + "/inpRows.json";
        this.expectedPreFile= folder + "/inpRowsExpected.json";
        this.postFile= folder + "/outRows.json";
        this.expectedpostFile= folder + "/outRowsExpected.json";
    }

    @Test
    /**
     * Simulates the calling of a handler    * 
     * Setup @Parameters with a list of folders containing the test files.  A test is performed for each entry
     * @throws CallbackHandlerException
     * @throws IOException
     * @throws JSONException
     * @throws SecurityException
     * @throws NoSuchFieldException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public void testHandler() throws Exception {

    /**********Set the expected results for the mocked methods****************/
        Mockito.when(mockHttpClient.execute(mockHttpPost)).thenReturn(mockHttpResponse); 
        Mockito.when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity);              
        Mockito.when(mockHttpEntity.getContent()).thenReturn(mockInputStream);
        PowerMockito.whenNew(InputStreamReader.class).withArguments(mockInputStream).thenReturn(mockInputStreamReader);
        PowerMockito.whenNew(BufferedReader.class).withArguments(mockInputStreamReader).thenReturn(mockBufferedReader);
        PowerMockito.whenNew(JSONObject.class).withArguments(Matchers.any(String.class)).thenReturn(jsonStub);

        searchHandler.pre(); //Call the actual Pre method to be tested

    }
}

有什么建议为什么我收到这个错误?

谢谢

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有