0

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)
Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33
coder123
  • 461
  • 2
  • 9
  • 14
  • 1
    most likely, your client is not being autowired. what line is throwing the error? – Stultuske May 04 '20 at 08:31
  • `abcRestClient` seems to be un-initialized – azro May 04 '20 at 08:32
  • @Stultuske `String response = abcRestClient.searchABCCodes(abc_code);' in ABCValidator.java. But I'm calling this through a controller and it is working as expected. – coder123 May 04 '20 at 08:33
  • @coder123 yes, abcRestClient is intialized when you execute your application, but not when you run your test, that's your problem – Stultuske May 04 '20 at 08:34

1 Answers1

0

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})

Nikita
  • 94
  • 1
  • 3