3

I have the file allDepartments.json in a subdirectory called fixtures, to which I want to access from the Fixture.java class.

enter image description here

This is my Fixture.java code:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public final class Fixture {
    private static final String FIXTURES_PATH = "";
    private final String fixture;

    public Fixture(String fixtureName) throws IOException {
        fixture = new String(Files.readAllBytes(Paths.get(FIXTURES_PATH + fixtureName)));
    }

    public final String getFixture() {
        return fixture;
    }
}

However every time he tries to access the file I get a java.nio.file.NoSuchFileException: allDepartments.json...

I have heard of the getResource() method and tried every combination possible of it, without success.

I need this to store multi-line strings for my JUnit tests.

What can I do?

PedroD
  • 5,670
  • 12
  • 46
  • 84
  • Is this file a resource? Is it packaged into the final JAR? – Tunaki Jan 15 '16 at 13:39
  • No, well, hope not. I want to use this just for the unitary tests (which can be run in Eclipse or during a maven install procedure). – PedroD Jan 15 '16 at 13:40
  • Ah so you have a Maven project and this file is under `src/test/resources`? (I can't see your screenshot if that's what it is) – Tunaki Jan 15 '16 at 13:41
  • Sort of. I am not using `src/test/resources`, would it solve my issue? – PedroD Jan 15 '16 at 13:43

4 Answers4

3

The NIO.2 API can't be used to read files that are effectively project resources, i.e. files present on the classpath.

In your situation, you have a Maven project and a resource that you want to read during the unit test of the application. First, this implies that this resources should be placed under src/test/resources so that Maven adds it automatically to the classpath during the tests. Second, this implies that you can't use the Files utility to read it.

You will need to resort to using a traditional BufferedReader:

public Fixture(String fixtureName) throws IOException {
    try (BufferedReader br = new BufferedReader(new InputStreamReader(Fixture.class.getResourceAsStream(FIXTURES_PATH + fixtureName)))) {
        // do your thing with br.readLine();
    }
}

Note the path given to getResourceAsStream is either relative to the current class or absolute. If the resources is located in src/test/resources/folder/allDepartments.json then a valid path would be /folder/allDepartments.json.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

Add allDepartments.json to the.classpath file of the project and java should be able to pick it up.

Refer this topic if you want to know how to add a file to class path from eclipse

Community
  • 1
  • 1
codeMan
  • 5,730
  • 3
  • 27
  • 51
1

when you run Fixtures.java the relative path would be

../fixtures/allDepartments.json

try using this path.

1

Thank you all for helping and suggestions.

Thanks to you I was able to put things working, so here is the trick (which I guess only works for Maven projects):

I moved the allDepartments.json file to the default src/test/resources folder as suggested by you guys. I didn't even had to modify the pom.xml. And now everything works!

So this is my project structure now:

enter image description here

And the final Fixture.java code is:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

public final class Fixture {
    private final String fixture;

    public Fixture(String fixtureName) throws IOException {
        fixture = this.readFile(fixtureName);
    }

    private String readFile(String fileName) throws IOException {
        final InputStream in = this.getClass().getClassLoader().getResource("fixtures/" + fileName).openStream();
        final BufferedReader buffer = new BufferedReader(new InputStreamReader(in));
        try {
            return buffer.lines().collect(Collectors.joining("\n"));
        } finally {
            buffer.close();
        }
    }

    public final String getFixture() {
        return fixture;
    }
}
PedroD
  • 5,670
  • 12
  • 46
  • 84