'FileNotFoundException in JUnit testing

I have this line of code in my program

Map jsonFile = new Gson().fromJson(new FileReader("Metro_Transit_Bus_Stops.geojson"), Map.class);

When I run it from the main of the class I get no errors, it is able to read and parse through the json file.

 public static void main(String[] args) {
        BSSDataWrangler dw = new BSSDataWrangler();
        List<Object> busStops = dw.createBusStop();
        System.out.println(busStops.size());
    }

However, when I call the method from my JUnit test method, 

@Test
public void test1() {
        
BSSDataWrangler dw = new BSSDataWrangler();
        List<Object> busStops = dw.createBusStop();
        assertEquals(25, busStops.size());

    }

it gives me this error. Any idea why? Thank you!

java.io.FileNotFoundException: Metro_Transit_Bus_Stops.geojson (The system cannot find the file specified)


Solution 1:[1]

It seems that the compiler is not able to find the file that you have specified. You can try sharing the absolute path of the file from the src folder of the project.

in this line.

Map jsonFile = new Gson().fromJson(new FileReader("Metro_Transit_Bus_Stops.geojson"), Map.class);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gurkirat Singh Guliani