本文介紹了在Java junit中創建壓縮文件的測試用例的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我被編寫了一個方法,該方法接受一個文件,然后在它創建一個壓縮文件之后。一旦壓縮文件創建完成,它就會嘗試將其存儲在GCS存儲桶中,并從臨時目錄中刪除這些文件。
有人能幫我寫一個測試用例嗎?
private void createZip(File file) throws IOException {
File zipFile = new File(System.getProperty("java.io.tmpdir"), Constants.ZIP_FILE_NAME);
if (!file.exists()) {
logger.info("File Not Found For To Do Zip File! Please Provide A File..");
throw new FileNotFoundException("File Not Found For To Do Zip File! Please Provide A File..");
}
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
FileInputStream fis = new FileInputStream(file);) {
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
}
try {
// store to GCS bucket
GcpCloudStorageUtil.uploadFileToGcsBucket(zipFile, zipFile.getName());
file.delete();
zipFile.delete();
} catch (Exception e) {
logger.error("Exception" + e);
throw new FxRuntimeException("Exception " + e.getMessage());
}
}
推薦答案
作為設計測試用例的一般經驗法則,您希望同時考慮一切正常時的預期行為和可能出錯的每種情況的預期行為。
對于您的特定問題,當一切正常時,您應該希望您的方法壓縮作為參數給定的文件,上載它并從臨時目錄中刪除該文件。
您可以通過監視靜態GcpCloudStorageUtil方法來做到這一點。間諜是類的實例,除非指定以及您可以監視哪些方法調用,否則將保留其通常的行為。您可以閱讀主題here和here(用于靜態方法間諜)的更多信息。
在沒有訪問其余代碼的情況下,很難給出完整的答案,但我建議:
創建非空文件
對此文件調用createZip方法
驗證是否調用了GcpCloudStorageUtil.ploadFileToGcsBucket方法
驗證上載的文件內容是否與您創建的文件匹配
正在驗證臨時目錄現在是否為空
也有很多方面可能會出錯。例如,作為參數提供的文件可能不存在。您想要編寫一個測試用例,以確保在這種情況下拋出FileNotFoundException。本article介紹了使用JUnit4或JUnit5執行此操作的不同方法。
類似地,您可以設計一個測試用例,在其中模擬GcpCloudStorageUtil.ploadFileToGcsBucket并強制它拋出異常,然后驗證是否拋出了預期的異常。模仿方法意味著強制它以某種方式運行(在這里,拋出異常)。同樣,您可以閱讀主題here的更多內容。
編輯:這是一個可能的測試類。
@SpringBootTest
public class ZipperTest {
MockedStatic<GcpCloudStorageUtil> mockGcpCloudStorageUtil;
@Before
public void init(){
mockGcpCloudStorageUtil = Mockito.mockStatic(GcpCloudStorageUtil.class);
}
@After
public void clean(){
mockGcpCloudStorageUtil.close();
}
@Test
public void testFileIsUploadedAndDeleted() throws IOException, FxRuntimeException {
//mocks the GcpCloudStorageUtil class and ensures it behaves like it normally would
mockGcpCloudStorageUtil.when(() -> GcpCloudStorageUtil.uploadFileToGcsBucket(Mockito.any(File.class), Mockito.anyString())).thenCallRealMethod();
Zipper zipper = new Zipper();
//creates a file
String content = "My file content";
Files.write(Paths.get("example.txt"),
List.of(content),
StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
zipper.createZip(new File("example.txt"));
//verifies that the uploadFileToGcsBucket method was called once
mockGcpCloudStorageUtil.verify(()->GcpCloudStorageUtil.uploadFileToGcsBucket(Mockito.any(File.class), Mockito.anyString()), times(1));
//you can insert code here to verify the content of the uploaded file matches the provided file content
//verifies that the file in the temp directory has been deleted
Assert.assertFalse(Files.exists(Paths.get(System.getProperty("java.io.tmpdir")+"example.txt")));
}
@Test(expected = FileNotFoundException.class)
public void testExceptionThrownWhenFileDoesNotExist() throws FxRuntimeException, IOException {
mockGcpCloudStorageUtil.when(() -> GcpCloudStorageUtil.uploadFileToGcsBucket(Mockito.any(File.class), Mockito.anyString())).thenCallRealMethod();
Zipper zipper = new Zipper();
zipper.createZip(new File("doesnt_exist.txt"));
//verifies that the uploadFileToGcsBucket method was not called
mockGcpCloudStorageUtil.verifyNoInteractions();
}
@Test(expected = FxRuntimeException.class)
public void testExceptionThrownWhenIssueGcpUpload() throws IOException, FxRuntimeException {
//this time we force the uploadFileToGcsBucket method to throw an exception
mockGcpCloudStorageUtil.when(() -> GcpCloudStorageUtil.uploadFileToGcsBucket(Mockito.any(File.class), Mockito.anyString())).thenThrow(RuntimeException.class);
Zipper zipper = new Zipper();
//creates a file
String content = "My file content";
Files.write(Paths.get("example.txt"),
List.of(content),
StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
zipper.createZip(new File("example.txt"));
mockGcpCloudStorageUtil.verify(()->GcpCloudStorageUtil.uploadFileToGcsBucket(Mockito.any(File.class), Mockito.anyString()), times(1));
}
}
這篇關于在Java junit中創建壓縮文件的測試用例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,