更新时间:2023-11-19 23:53:04
作为设计测试用例的一般经验法则,您希望同时考虑一切正常时的预期行为和可能出错的每种情况的预期行为。
对于您的特定问题,当一切正常时,您应该希望您的方法压缩作为参数给定的文件,上载它并从临时目录中删除该文件。 您可以通过监视静态GcpCloudStorageUtil方法来做到这一点。间谍是类的实例,除非指定以及您可以监视哪些方法调用,否则将保留其通常的行为。您可以阅读主题here和here(用于静态方法间谍)的更多信息。
在没有访问其余代码的情况下,很难给出完整的答案,但我建议:
也有很多方面可能会出错。例如,作为参数提供的文件可能不存在。您想要编写一个测试用例,以确保在这种情况下抛出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));
}
}