英文:
TestNG giving NullPointerException
问题
我正在使用TestNG验证一个方法,并且遇到了以下`空指针异常`。
**失败:** testValidateABC
java.lang.NullPointerException
在packageA.ABCValidator.validateABC(ABCValidator.java:22)中
在packageA.ABCValidatorTest.testValidateABC(ABCValidatorTest.java:25)中
在sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)中
在sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)中
在java.lang.reflect.Method.invoke(Unknown Source)中
在org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)中
在org.testng.internal.Invoker.invokeMethod(Invoker.java:639)中
在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)中
在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)中
在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)中
在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)中
在org.testng.TestRunner.privateRun(TestRunner.java:774)中
在org.testng.TestRunner.run(TestRunner.java:624)中
在org.testng.SuiteRunner.runTest(SuiteRunner.java:359)中
在org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)中
在org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)中
在org.testng.SuiteRunner.run(SuiteRunner.java:261)中
在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)中
在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)中
在org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)中
在org.testng.TestNG.runSuitesLocally(TestNG.java:1140)中
在org.testng.TestNG.run(TestNG.java:1048)中
在org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)中
在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)中
在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;
}
}
我以为我之所以收到`空指针异常`是因为某些空值在进行处理,所以我在ABCValidatorTest中处理了这个问题,只有在不为空的情况下才调用了那个方法。
**编辑:** 通过添加以下内容更新了ABCValidatorTest.java
@ContextConfiguration(classes = {ABCValidator.class})
现在收到以下错误:
org.testng.TestNGException: java.net.UnknownHostException: testng.org
在org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:320)中
英文:
I am validating a method with TestNG and getting the below nullpointer exception
.
FAILED: 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 (Running this thorugh TestNG to get test results)
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 (Testing the method present in this class)
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;
}
}
I thought I was getting Nullpointer Exception
because some null value was going thorugh, so i handled it in ABCValidatorTest by calling that method only if it is not empty.
EDIT: Updated ABCValidatorTest.java by adding
@ContextConfiguration(classes = {ABCValidator.class})
Getting the below error now:
org.testng.TestNGException: java.net.UnknownHostException: testng.org
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:320)
答案1
得分: 0
如果您想使用Spring依赖注入(@Autowired),您应该使用Spring上下文运行您的测试。
@ContextConfiguration(classes = {您想要使用的类})
如果Spring Boot应该对您的测试类进行注解,
@SpringBootTest(classes = {您想要使用的类})
英文:
If you want to use spring dependency injection (@Autowired) you should run your tests with spring context.
@ContextConfiguration(classes = {classes you want to use})
If spring boot should annotate your test class with
@SpringBootTest(classes = {classes you want to use})
专注分享java语言的经验与见解,让所有开发者获益!
评论