我正在使用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。
答案 0 :(得分:1)
以下是罗格里奥的正确答案。请注意,答案仅显示一个测试用例。
@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);
}