In every project that I start I’m always fussing with getting a file from the classpath when testing some code. For example, when I have to get an XML file and compare it with some test result. I always end up with a method called getResourceAsAString or something similar and I always have to do some googling on how to do it. So without further ado, my latest fancy getResourceAsAString method:

package nl.confused

import org.springframework.util.ResourceUtils;
import java.nio.file.Files;

public class TestUtils {

    public static String getResourceAsAString(String fileLocation) throws IOException {
        return new String(Files.readAllBytes(ResourceUtils.getFile(fileLocation).toPath()));

And this is the actual call of the method: