英文:
How to write a junit Test Case of a function which is reading a file?
问题
以下是您的代码的中文翻译:
这是我的代码:
public String getFile() throws IOException {
StringBuilder fileURI = new StringBuilder(locationName);
fileURI.append(keyName);
S3ObjectInputStream ts = null;
ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURI.toString());
String time;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(ts))) {
time = reader.readLine();
}
return time;
}
我试图对这个函数进行模拟,但不确定如何模拟InputStreamReader对象:
public void testgetTs() throws IOException {
Mockito.mock(Service.class);
StringBuilder fileURI = new StringBuilder("test");
fileURI.append("/");
fileURI.append("testFile.txt");
S3ObjectInputStream ts = null;
Mockito.when(s3Services.getResourceStream(fileURI.toString())).thenReturn(ts);
service.getTime();
}
请注意,我已经按照您的要求,只提供代码部分的翻译。如果您有其他问题或需要进一步的帮助,请随时提出。
英文:
Here is my code :
public String getFile() throws IOException {
StringBuilder fileURI = new StringBuilder(locationName);
fileURI.append(keyName);
S3ObjectInputStream ts = null;
ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURI.toString());
String time;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(ts))) {
time = reader.readLine();
}
return time;
}
I am trying to mock this function, I am not sure how to Mock the InputStreamReader object :
public void testgetTs() throws IOException {
Mockito.mock(Service.class);
StringBuilder fileURI = new StringBuilder("test");
fileURI.append("/");
fileURI.append("testFile.txt");
S3ObjectInputStream ts = null;
Mockito.when(s3Services.getResourceStream(fileURI.toString())).thenReturn(ts);
service.getTime();
}
答案1
得分: 1
你需要模拟所有外部资源,对于你的情况来说这很困难,因为你在内部创建所有资源,因此你的方法没有单一职责,而是有许多职责。为了进行适当的单元测试,你需要将其分解为单元,注入参数并模拟资源:
单元1,构建文件名
StringBuilder fileURI = new StringBuilder(locationName);
fileURI.append(keyName);
在这里,你希望能够测试将locationName
和keyName
设置为不同值时的结果。例如像FileUriBuilder(locationName, keyName).getString()
这样的操作。
单元2,获取资源流
S3ObjectInputStream ts = null;
ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURI.toString());
在这里,你希望模拟s3Services
并测试是否使用预期值调用了getResourceStream()
。如果你正在注入FileUriBuilder
,你也需要模拟它。
单元3,读取流
String time;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(ts))) {
time = reader.readLine();
}
return time;
在这里,你希望模拟S3ObjectInputStream
,确保正确调用了其上的方法,并且当它返回内容时,这个单元将其报告为预期结果。
英文:
You need to mock out any external resources, in your case that's hard because you are creating all your resources internally and therefore your method does not have a single responsibility, but many. To properly unit test you would need to break it into units, inject parameters and mock out resources:
Unit 1, Building the file name
StringBuilder fileURI = new StringBuilder(locationName);
fileURI.append(keyName);
Here you want to be able to test what the result of setting locationName
and keyName
to different things is. i.e. something like FileUriBuilder(locationName, keyName).getString()
Unit 2, Getting the resource stream
S3ObjectInputStream ts = null;
ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURI.toString());
Here you want to mock out s3Services
and test that getResourceStream()
is called with the expected value. If you are injecting FileUriBuilder
you'll want to mock that.
Unit 3, Reading the stream
String time;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(ts))) {
time = reader.readLine();
}
return time;
Here you want to mock out S3ObjectInputStream
and make sure the correct methods on it are called and that when it returns something that this unit reports it as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论