我正在尝试模拟以下功能
public class ServerConnection{
public ServerConnection(ClientConnection cn) {
super(cn);
}
public void setUrl(String url) {
this.url = URLUtil.processURL(url);
try {
URL dst = new URL(this.url);
InputStream is = dst.openStream();
Scanner scanner = new Scanner(is);
StringBuilder sb = new StringBuilder();
while(scanner.hasNextLine())
sb.append(scanner.nextLine()).append("\n");
if (validate(sb.toString())) {
--------
} else { }
is.close();
scanner.close();
} catch (Exception ex) {
}
}
private boolean validate(String content) {
JSONParser parser = new JSONParser();
Boolean isJsonValid = false;
JSONObject json = null;
try {
--------
//json validation goes here
} catch (Exception e) {
}
return isJsonValid;
}
public void setId(Integer id) {
if(id == null)
this.id = 0;
else
this.id = id;
}
}
PowerMockito Junit代码
@RunWith(PowerMockRunner.class)
@PrepareForTest({PathTest.class })
public class URLTest {
ServerConnection sc ;
String URL = "http://test.com";
@Before
public void setUp() throws Throwable{
ClientConnection con =PathTest.getCon(); // Here getCon() is a static method
sc = new ServerConnection(con);
sc.setId(1000);
}
@Test
public void testName() throws Throwable {
String expectedResponse = "test";
URL url = PowerMockito.mock(URL.class);
HttpURLConnection connection = PowerMockito.mock(HttpURLConnection.class);
InputStream inputStream = PowerMockito.mock(InputStream.class);
Scanner scanner = PowerMockito.mock(Scanner.class);
PowerMockito.whenNew(URL.class).withArguments(URL).thenReturn(url);
PowerMockito.whenNew(Scanner.class).withArguments(inputStream).thenReturn(scanner);
PowerMockito.when(scanner.useDelimiter("\\A")).thenReturn(scanner);
PowerMockito.when(url.openConnection()).thenReturn(connection);
// Response code mocked here
PowerMockito.when(connection.getResponseCode()).thenReturn(200);
PowerMockito.when(connection.getInputStream()).thenReturn(inputStream);
PowerMockito.when(scanner.hasNext()).thenReturn(true);
PowerMockito.when(scanner.next()).thenReturn(expectedResponse);
sc.setUrl(URL);
}
}
在Iam执行此操作时,我观察到以下错误消息
URLTest
com.objects.URLTest
testName(com.objects.URLTest)
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at javassist.runtime.Desc.getClassObject(Desc.java:43)
at javassist.runtime.Desc.getClassType(Desc.java:152)
at javassist.runtime.Desc.getType(Desc.java:122)
at javassist.runtime.Desc.getType(Desc.java:78)
at com.objects.PathTest.getCon(TargetPathTest.java:24)
at com.objects.URLTest.setUp(URLTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:133)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)
Caused by: java.lang.NullPointerException
... 38 more
答案 0 :(得分:2)
此代码有很多错误。
第一个技术答案是:您似乎不知道自己在做什么。您有@PrepareForTest({PathTest.class })
,它指示您打算模拟该类的静态方法。
但是那样的话,您就没有做必要的事情来模拟该类中的静态方法。只需逐步遵循他们的documentation。我还认为URL类是最终的,因此您还必须准备并注释该类,以便将其与whenNew()一起使用!
但是,然后:您应该避免模拟URL或URL连接对象。只需使用某种依赖注入,并确保您可以将一些模拟实例传递到测试中的代码中,例如使用Mockito的@InjectMocks批注。从那里,您还可以避免使用 static 方法。
长话短说:您的生产代码编写错误,并且(诚实地)您的单元测试简直太恐怖了。您应该认真地回到这里,然后把那些东西扔掉。然后重新考虑您的生产代码,以不直接使用new
,并且不要依赖 static 方法。这样,您可以摆脱PowerMockito的困扰,而可以使用普通的Mockito(嗯,您需要启用final类的模拟)。但是请放心:除非绝对必要,否则使用PowerMock(ito)没有任何意义。
然后:不要“发明”模拟代码。阅读教程,并逐步遵循它们。在测试用例中,您的模拟就像 everything 一样,但是您应该做完全相反的事情:仅在绝对没有其他方法可以测试代码时才模拟事物。而且,当这种方法破坏了合理地对代码进行单元测试的能力时,您绝对不能将方法设为静态。
(static有它的位置,但是当它妨碍您时,您就在做错事!)