반응형
테스트 리소스 파일을 얻는 방법은 무엇입니까?
단위 테스트에서 csv 파일을 가져와야합니다. 리소스 폴더, 즉 src / test / resources에 있습니다.
예를 들어 단위 테스트를 수행 할 때 파일을 사용할 수있는 경우 유용 할 것입니다. 이것은 jar AFAIK에서 파일을로드하지 않습니다.
URL url = Thread.currentThread().getContextClassLoader().getResource("mypackage/YourFile.csv");
File file = new File(url.getPath());
// where the file is in the classpath eg. <project>/src/test/resources/mypackage/YourFile.csv
현재 스레드의 클래스 로더를 사용하여 테스트 리소스에 액세스 할 수 있습니다.
InputStream stream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("YOURFILE.CSV");
구아바와 함께
import com.google.common.io.Resources;
URL url = Resources.getResource("YourFile.csv");
// assuming a file src/test/resources/some-file.csv exists:
import java.io.InputStream;
// ...
InputStream is = getClass().getClassLoader().getResourceAsStream("some-file.csv");
import org.apache.commons.io.FileUtils;
...
final File dic = FileUtils.getFile("src","test", "resources", "csvFile");
이 솔루션은 lib가 필요하지 않습니다. 먼저 리소스 파일에 액세스하기위한 util 클래스를 만듭니다.
public class TestUtil(Class classObj, String resourceName) throws IOException{
URL resourceUrl = classObj.getResource(FileSystems.getDefault().getSeparator()+resourceName);
assertNotNull(resourceUrl);
return new File(resourceUrl.getFile());
}
이제 unitTest의 클래스와 ressource 폴더에있는 파일 이름으로 메서드를 호출하면됩니다.
File cvsTestFile = TestUtil.GetDocFromResource(getClass(), "MyTestFile.cvs");
참조 URL : https://stackoverflow.com/questions/5529532/how-to-get-a-test-resource-file
반응형
'code' 카테고리의 다른 글
'double_scalars에서 잘못된 값이 발견되었습니다'경고, 아마도 numpy (0) | 2020.12.24 |
---|---|
False 또는 없음 vs. 없음 또는 False (0) | 2020.12.24 |
애플리케이션 버전에 따라 Inno Setup 설치 프로그램의 버전을 자동으로 설정하려면 어떻게해야합니까? (0) | 2020.12.24 |
jQuery get position of element relative to another element (0) | 2020.12.24 |
Git 푸시 오류 : "origin이 git 저장소로 보이지 않습니다." (0) | 2020.12.24 |