メソッドに提供されたパスが存在する場合、ファイルを取得する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;
}
は、私はファイルが存在しないときにメソッドが例外をスローするテストをチェックするJunit
テストを書きたいとパスがないファイルが、ディレクトリである場合(条件は、ifループでカバー)。
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()
で2つのシナリオをテストする方法を理解できません。ファイルが存在しない場合、またはファイルパスがディレクトリの場合、例外がスローされるようにします。
ニースを@Slackmartおかげで、私はまた、ユニットテストに優先順位を持つアンドロイドを学んでいます。 – slackmart