在Amazons中使用Junit和Mockito进行GetObject方法的单位测试3
我有使用s3.GetObject获取S3Object的方法,并将对象的内容写入临时文件中,
@Override
public Optional<String> getObject(String s3BucketName, String s3Path) {
try {
S3Object s3Object = s3Client.getObject(new GetObjectRequest(s3BucketName, s3Path));
try (S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent()){
File tmp = File.createTempFile("/tmp/" + UUID.randomUUID().toString(), ".json");
IOUtils.copy(s3ObjectInputStream, new FileOutputStream(tmp));
return Optional.of(tmp.getAbsolutePath());
} catch (Exception e) {
System.err.println(e.getMessage());
}
} catch (AmazonServiceException e) {
String msg = String.format("Service error while getting object=%s in bucket=%s",
s3Path, s3BucketName);
throw new RuntimeException(msg, e);
} catch (SdkClientException e) {
String msg = String.format("Client error while getting object=%s in bucket=%s",
s3Path, s3BucketName);
throw new RuntimeException(msg + e.getMessage());
}
return Optional.empty();
}
我不确定我知道如何为此方法编写单元测试。这是我尝试过的方法,
@Test
public void getObjectTest() throws UnsupportedEncodingException {
S3Object s3Object = Mockito.mock(S3Object.class);
s3Object.setObjectContent(new StringInputStream(TEST_STRING));
Mockito.when(mockS3Client.getObject(new GetObjectRequest(TEST_S3BUCKET, TEST_S3OBJECT))).thenReturn(s3Object);
s3Accessor.getObject(TEST_S3BUCKET, TEST_S3OBJECT);
verify(mockS3Client).getObject(new GetObjectRequest(TEST_S3BUCKET, TEST_S3OBJECT));
}
我是对单元测试的新手,我不确定我可以在这里断言什么,因为我只从该方法中获得了文件的绝对路径。 有人可以建议我吗?
I have method which uses s3.getObject to get S3Object and writes the contents of the object into a temporary file
@Override
public Optional<String> getObject(String s3BucketName, String s3Path) {
try {
S3Object s3Object = s3Client.getObject(new GetObjectRequest(s3BucketName, s3Path));
try (S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent()){
File tmp = File.createTempFile("/tmp/" + UUID.randomUUID().toString(), ".json");
IOUtils.copy(s3ObjectInputStream, new FileOutputStream(tmp));
return Optional.of(tmp.getAbsolutePath());
} catch (Exception e) {
System.err.println(e.getMessage());
}
} catch (AmazonServiceException e) {
String msg = String.format("Service error while getting object=%s in bucket=%s",
s3Path, s3BucketName);
throw new RuntimeException(msg, e);
} catch (SdkClientException e) {
String msg = String.format("Client error while getting object=%s in bucket=%s",
s3Path, s3BucketName);
throw new RuntimeException(msg + e.getMessage());
}
return Optional.empty();
}
I'm not sure I understand how to write the unit test for this method. Here is what I have tried
@Test
public void getObjectTest() throws UnsupportedEncodingException {
S3Object s3Object = Mockito.mock(S3Object.class);
s3Object.setObjectContent(new StringInputStream(TEST_STRING));
Mockito.when(mockS3Client.getObject(new GetObjectRequest(TEST_S3BUCKET, TEST_S3OBJECT))).thenReturn(s3Object);
s3Accessor.getObject(TEST_S3BUCKET, TEST_S3OBJECT);
verify(mockS3Client).getObject(new GetObjectRequest(TEST_S3BUCKET, TEST_S3OBJECT));
}
I'm new to unit tests and I'm not sure what I can assert here since I'm getting only an absolute path of the file from the method.
Can someone advice me on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议在测试中有一些建议
请勿模拟
s3Object
而不是从本地文件构建对象,将值设置为模拟对象不正确。您也应该为例外添加测试。
查看以下代码
There are a few things I would suggest changing in the test
Do not mock the
S3Object
rather build the object from a local file, setting the value to the mocked object isn't correct.You should add tests for exceptions as well.
Check out the below code