【问题标题】:jmockit of HttpURLConnection throws NullPointerExceptionHttpURLConnection 的 jmockit 抛出 NullPointerException
【发布时间】:2015-09-18 12:16:51
【问题描述】:

我正在使用 JMockit 来模拟 HttpURLConnection。我最初的两个测试用例(fileNotFoundResponse、badMimeType)可以找到,但我的第三个测试用例(contentDisposition)在 Expectations 块中生成 NPE,我不知道为什么。

这里是 Junit 测试类:

@RunWith(JMockit.class)
public class TestHttpGET1 { @Before
    public void setUp() throws Exception {
    }    

    @After
    public void tearDown() throws Exception {
    }

    public final class MockURL extends MockUp<URL> {
        @Mock
        public void $init(String strURL) {}
        @Mock
        public URLConnection openConnection() throws IOException {
        //System.out.println(">>mockURL.openConnection");
            return mockCon;
        }   
    }

    @Mocked HttpURLConnection mockCon;
    @Mocked LoggerFactory mockLoggerFactory;
    @Test
    public void fileNotFoundResponse() {
//      new MockUp<URL> (){
//          @Mock
//          public void $init(String strURL) {}
//          @Mock
//          public URLConnection openConnection() throws IOException {
//              //System.out.println(">>mockURL.openConnection");
//              return mockCon;
//          }
//      };
        new MockURL();
        String contents = "";
        try {
            new NonStrictExpectations() {{
                    LoggerFactory.getLogger(TestHttpGET1.class); result = null;
                mockCon.getResponseCode(); result = 404;
            }};
            contents = Main.httpGETFile("http://bogus");
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertEquals("Contents should be null", null, contents);    
    }

    @Test
    public void badMimeType() {
        new MockURL();
        String contents = "";
        try {
            new NonStrictExpectations() {{
                    mockCon.getResponseCode(); result = 200;
                    mockCon.getHeaderField("Content-Disposition"); result = null;
                    mockCon.getContentType(); result = "bogus-mime-type";
                    mockCon.getContentLength(); result = 1000;
            }};
            contents = Main.httpGETFile("http://bogus.m3u8");
        } catch (IOException e) {
            e.printStackTrace();
    }
        assertEquals("Contents should be null", null, contents);
    }


    @Test
    public void contentDisposition() {
        new MockURL();
        String contents = "";
        try {
            new Expectations() {{
                    String str = "Hello World";
                    mockCon.getResponseCode(); result = 200;
                    mockCon.getHeaderField("Content-Disposition"); result = "filename=test.m3u8";
                    mockCon.getContentType(); result = "application/x-mpegURL";
                    mockCon.getContentLength(); result = 11;
System.out.println("getContentLength");
                    mockCon.getInputStream(); result = (InputStream) new ByteArrayInputStream(str.getBytes("UTF8"));
System.out.println("getInputStream");
            }};
            contents = Main.httpGETFile("http://bogus.m3u8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertEquals("Contents should match", "Hello World", contents);
    }

}

NPE 是在这一行生成的:

                    mockCon.getInputStream(); result = (InputStream) new ByteArrayInputStream(str.getBytes("UTF8"));

这是堆栈跟踪:

java.lang.NullPointerException
    at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:237)
    at java.net.URLConnection.getInputStream(URLConnection.java)
    at edu.psu.gv.sweng861.TestHttpGET1$3.<init>(TestHttpGET1.java:97)
    at edu.psu.gv.sweng861.TestHttpGET1.contentDisposition(TestHttpGET1.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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)

我不知道为什么代码在 java.net.URLClassLoader.getResourceAsStream 中执行。

我正在使用最新版本的 JMockit v.1.19。

【问题讨论】:

  • @Mocked(不是MockUp)也用于URL,那么它应该可以工作。
  • 罗杰里奥 - 你说的太对了!我发布了您的修复程序供所有人查看。

标签: mocking httpurlconnection jmockit


【解决方案1】:

这是来自 Rogério 的正确答案。请注意,答案仅显示一个测试用例。

    @Mocked URL mockURL;
    @Mocked HttpURLConnection mockCon;
    @Mocked LoggerFactory mockLoggerFactory;

    @Test
    public void contentDisposition() {
        //new MockURL();
        String contents = "";
        try {
            new Expectations() {{
                    String str = "Hello World";
                    mockCon.getResponseCode(); result = 200;
                    mockCon.getHeaderField("Content-Disposition"); result = "filename=test.m3u8";
                    mockCon.getContentType(); result = "application/x-mpegURL";
                    mockCon.getContentLength(); result = 11;
System.out.println("getContentLength");
                    mockCon.getInputStream(); result = (InputStream) new ByteArrayInputStream(str.getBytes("UTF8"));
System.out.println("getInputStream");
            }};
            contents = Main.httpGETFile("http://bogus.m3u8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertEquals("Contents should match", "Hello World\n", contents);
    }

【讨论】:

    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多