Home > OS >  NullPointerException from java unit test
NullPointerException from java unit test

Time:08-05

The method I want to test :

public JSONArray weatherRequest(double lon, double lat) {
    try {
        //Public Api:
        // https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}
        //access_key = ***
        URL url = new URL("https://api.openweathermap.org/data/2.5/weather?lat="   lat   ".48&lon="   lon   "&appid=***");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.connect();
        //Check if connect is made
        int responseCode = conn.getResponseCode();
        // 200 OK
        if (responseCode != 200) {
            throw new RuntimeException("HttpResponseCode: "   responseCode);
        } else {
            StringBuilder informationString = new StringBuilder();
            Scanner scanner = new Scanner(url.openStream());
            while (scanner.hasNext()) {
                informationString.append(scanner.nextLine());
            }
            //Close the scanner
            scanner.close();

            //JSON simple library Setup with Maven is used to convert strings to JSON
            JSONParser parse = new JSONParser();
            Object obj = parse.parse(String.valueOf(informationString));
            JSONArray dataObject = new JSONArray();
            dataObject.add(obj);
            LOG.debug("JSON object: "   dataObject);
            return dataObject;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

Test Class :

public class MySecondAdapterTest {

    MySecondAdapter mySecondAdapter;

    @Test
        public void testWeatherRequest() {
        JSONArray result = mySecondAdapter.weatherRequest(53.483, -2.29);
        Assert.assertNotNull(result);
    }

}

My getter and setter tests are passing. I'm trying to figure out why the methods unit test is not passing? And any steps I could take to debug for myself in the future. Thanks

CodePudding user response:

Stack trace

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

CodePudding user response:

Solved by creating an object at the start of test:

MySecondAdapter mySecondAdapter = new MySecondAdapter();
  • Related