유닛 테스트에서 텍스트 파일을 읽었으며 리소스 폴더에 일부 입력 텍스트 파일을 배치했습니다. 다음은 디렉토리 구조입니다.JUnit 테스트에서 리소스 파일 읽기
- SRC -> COM -> AU -> MyApp를 -> 폴더의 유틸리티 -> MyFileReader
- 테스트 -> COM -> AU -> MyApp를 -> 폴더의 유틸리티 -> MyFileReaderTest
- 테스트 -> COM -> AU -> MyApp를 -> 자원 ->
주 input.txt를 SRC 테스트는 동일한 계층 구조에 있는지.
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()
을 반환합니다. 도움을 주셔서 감사합니다.
.class를 사용하여 상대 경로를 찾을 수 있는지 확인하십시오. http://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource –
' classes' 클래스의'getResourceAsStream()'메소드를 사용할 수 있습니다. –