我寫了一個方法,它接受一個檔案,然后在它創建一個 zip 檔案之后。完成 zip 檔案創建后,它會嘗試將其存盤在 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());
}
}
uj5u.com熱心網友回復:
作為設計測驗用例時的一般經驗法則,您既要考慮一切正常時的預期行為,又要考慮可能出錯的每種方式的預期行為。
對于您的特定問題,當一切正常時,您應該期望您的方法壓縮作為引數給出的檔案,上傳它并從臨時目錄中洗掉檔案。您可以通過監視您的靜態 GcpCloudStorageUtil 方法來做到這一點。間諜是一個類的實體,除非指定并且您可以監控哪些方法呼叫,否則它們會保留其通常的行為。您可以在此處和此處閱讀有關該主題的更多資訊(用于靜態方法間諜)。
如果不訪問您的其余代碼,很難給出徹底的答案,但我建議:
- 創建一個非空檔案
- 對此檔案呼叫 createZip 方法
- 驗證是否呼叫了 GcpCloudStorageUtil.uploadFileToGcsBucket 方法
- 驗證上傳的檔案內容是否與您創建的檔案匹配
- 驗證臨時目錄現在是否為空
還有很多方法可能會出錯。例如,作為引數給出的檔案可能不存在。您想撰寫一個測驗用例,以確保在這種情況下拋出 FileNotFoundException。本文介紹了使用 JUnit4 或 JUnit5 執行此操作的不同方法。
同樣,您可以設計一個測驗用例,在其中模擬 GcpCloudStorageUtil.uploadFileToGcsBucket 并強制它拋出例外,然后驗證是否拋出了預期的例外。模擬一個方法意味著強制它以某種方式運行(這里,拋出一個例外)。同樣,您可以在此處閱讀有關該主題的更多資訊。
編輯:這是一個可能的測驗類。
@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));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412501.html
標籤:
上一篇:如何測驗是否單擊了反應組件
