我正在用TestNG验证一个方法,并得到下面的nullpointer exception。
失败: testValidateABC
java.lang.NullPointerException
at packageA.ABCValidator.validateABC(ABCValidator.java:22)
at packageA.ABCValidatorTest.testValidateABC(ABCValidatorTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)ABCValidatorTest.java (运行此TestNG以获得测试结果)
public class ABCValidatorTest {
@Test
public void testValidateABC() {
ABCValidator validator = new ABCValidator();
List<String> input = Arrays.asList("0", "1234", "abcd");
List<Boolean> expectedOutput = Arrays.asList(false, true, false);
boolean output;
for (int i = 0; i < input.size(); i++) {
if (StringUtils.isNotEmpty(input.get(i))) {
output = validator.validateABC(input.get(i));
} else {
output = false;
}
Assert.assertEquals((boolean) expectedOutput.get(i), output);
}
}
}ABCValidator.java (测试该类中的方法)
public class ABCValidator {
@Autowired(required = false)
private ABCSearch abcRestClient;
public boolean validateABC(String abc_code) {
String response = abcRestClient.searchABCCodes(abc_code);
boolean hasValidabc = true;
if ((response.startsWith("null", 1)) || (response.equals("[]"))) {
hasValidabc = false;
}
return hasValidabc;
}
}我以为我得到的是Nullpointer Exception,因为有些空值会变硬,所以我在ABCValidatorTest中处理它,只有在方法不是空的情况下才调用它。
编辑:通过添加更新ABCValidatorTest.java
@ContextConfiguration(classes = {ABCValidator.class})现在获取以下错误:
org.testng.TestNGException: java.net.UnknownHostException: testng.org
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:320)发布于 2020-05-04 08:35:04
如果您想要使用spring依赖注入(@Autowired),您应该使用spring上下文运行您的测试。@ContextConfiguration(类={要使用}的类)
如果spring引导应该用@SpringBootTest注释您的测试类( class ={类您想使用})
https://stackoverflow.com/questions/61588051
复制相似问题