I read text file in my unit test and I placed some input text files in resources folder. Following is the directory structure.
- src -> com -> au -> myapp -> util -> MyFileReader
- test -> com -> au -> myapp -> util -> MyFileReaderTest
- test -> com -> au -> myapp -> resources-> input.txt
Note that src and test are in the same hierarchy.
public class MyFileReaderTest
{
ClassLoader classLoader = getClass().getClassLoader();
@Test
public void testReadInputFile() throws Exception
{
String file = classLoader.getResource("test/com/au/myapp/resources/input.txt").getFile();
List<String> result = InputFileReader.getInstance().readFile(file);
assertEquals("Size of the list should be 2", 2, result.size());
}
}
Classloader.getResource()
returns null. Really appreciate your assistance.