java - 如何为采用文件路径并创建新文件的方法编写 Junit 测试?
问题描述
我在 Java 类中有一个方法,它采用文件路径并创建一个新文件。如何为此方法编写 Junit 测试?
public String sendToECM(String filePath) {
String ecmDocId = StringUtils.EMPTY;
try {
File file = new File(filePath);
if(file.exists()) {
DocumentPropertiesVO documentPropertiesVO = UploadFileToContentManagement.uploadDocument(file);
ecmDocId = documentPropertiesVO.getDocumentID();
}
return ecmDocId;
} catch (SomeException cee) {
log.error("There was an error adding the document", cee);
}
}
解决方案
@Test
void testValidFilePath() {
String filePath = "path.txt";
String str = sendToECM(filePath);
Assertions.assertNotEquals(str, ""); // or null, depending on what your StringUtils.EMPTY is doing
@Test
void testInvalidFilePath() {
String filePath = "invalidPath.txt";
String str = sendtoECM(filePath);
Assertions.assertEquals(str, StringUtils.EMPTY);
推荐阅读
- python - multiprocessing.Pool.map 卡在最后一个 Process
- sdl - SDL2 Segfaults - 实现渲染器
- android - 将 ViewModel 放入 AppComponent (Dagger2)
- angular - Angular NgForm form.resetForm 和 form.reset 不适用于数组
- javascript - 缺少.then,但不知道该放在哪里
- google-cloud-platform - 从 gcloud 中删除不需要的服务帐户拒绝所有者权限
- linux - 如何更改vscode中的光标高度?
- html - 如何在一行上设置两个 div - 我的代码出错?
- python - 从 np.array 中提取的互补索引不一致
- javascript - 如何从谷歌库运行 HTML?