메소드에 제공된 경로가있는 경우 파일을 제공하는 get 파일 메소드가 있습니다. 파일이 없으면 NullPointerException
이 표시됩니다.Mockito를 사용하여 예외 테스트
public File getFile(String downloadFileName) throws Exception {
File fileToUpload = new File(getFileLocation(downloadFileName));
if(!fileToUpload.exists() && !fileToUpload.isFile()){
throw new Exception("File not found");
}
return fileToUpload;
}
나는 파일이 존재하지 않는 경우 메소드가 예외를 throw하는 테스트를 확인하는 Junit
테스트를 작성하고 싶은 경로가 아닌 파일이 아니라 디렉토리 인 경우 (조건이 경우 루프에 포함).
Junit와 :
public class KnewtonContentInventoryWokerTest {
private HttpWorker httpHelper = mock(HttpWorker.class);
private KnewtonContentInventoryWorker knewtonContentInventoryHelper;
private MessageHandler messageHandler = spy(new MessageHandler(false));
private String programNameString = "Realize Sample Program";
private String completeResponse;
private String filePath = "src/test/resources/Content_Inventory_testFile.xls" ;
private String fileName = "Content_Inventory_testFile";
private String fileDir = "src/test/resources/";
private String jobIdRestPath = "";
private String jobStatusRestPath = "";
private String downloadExcelFileRestPath = "";
private String uploadFileToS3RestPath = "";
private File file = new File(filePath);
@Before
public void setup() throws Exception {
setupWorker();
}
@Before
public void setupWorker() {
AuthContext authContext = new AuthContext();
authContext.setAuthenticationDetails("/test/", "user", "pass", new HashSet<Cookie>());
File file = new File("src/test/resources/Content_Inventory_testFile.xls");
knewtonContentInventoryHelper = spy(new KnewtonContentInventoryWorker(authContext,programNameString, externalIds));
knewtonContentInventoryHelper.messageHandler = messageHandler;
}
/**
* Test the setting up method
*/
@Test(expected = Exception.class)
public void testGetFileThrowsException() throws Exception {
doThrow(new Exception("")).when(knewtonContentInventoryHelper).getFile(anyString());
knewtonContentInventoryHelper.getFile(anyString());
}
}
위 Junit
내가 그러나 나는 시험 권리를하고 있지 않다 생각, 지금 가지고있는 것입니다. 테스트 방법 testGetFileThrowsException()
에서 두 시나리오를 테스트하는 방법을 이해할 수 없습니다. 파일이 존재하지 않거나 파일 경로가 디렉토리 인 경우 예외가 throw되기를 원합니다.
니스 @Slackmart. – slackmart