junit和Mockito单位测试for Stream.mark(stream.abable)执行S3 PutObject时
我有一种将InputStream放入S3存储桶中的方法。
@Override
public String putObject(@NonNull String s3BucketName, @NonNull String s3Key, @NonNull String content,
@NonNull ObjectMetadata metadataRequest) {
InputStream stream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
try {
stream.mark(stream.available());
} catch (IOException e) {
String errorMessage = String.format("Runtime error while marking stream.",
s3BucketName, s3Key);
throw new RuntimeException(errorMessage, e);
}
PutObjectRequest request = new PutObjectRequest(s3BucketName, s3Key, stream, metadataRequest);
return putObject(request);
}
我想使方法引起ioexception,然后投掷Runtime Exception,然后为此编写了单位测试。
@Test
public void putObjectTest_withStringContent_withMetadataRequest_IOError() {
ObjectMetadata metadataRequest = new ObjectMetadata();
metadataRequest.addUserMetadata(TEST_METADATA_KEY, TEST_METADATA_VALUE);
InputStream mockStream = Mockito.mock(InputStream.class);
Mockito.when(mockStream.mark(mockStream.available())).thenThrow(IOException.class);
assertThrows(RuntimeException.class, () -> s3Accessor.putObject
(TEST_S3BUCKET, TEST_S3OBJECT, TEST_STRING, metadataRequest));
}
这是我尝试的方法,但是这显示了编辑器中的错误,
Required type:
T
Provided:
void
reason: no instance(s) of type variable(s) T exist so that void conforms to T
该如何使方法投掷ioexception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
stream.mark(...)
will 永远不要抛出任何检查过的例外无论如何,这是毫无意义的。您也无需手动标记或重置流。 SDK执行背后场景。
如果您的流大于128 kb,则您可能需要调用
setReadLimit
至1个字节大于流的大小,以避免使用resetexception
当不使用> fileInputStream
。如果不是,那么您无需做任何事情。无论如何,您不需要自己标记或重置流。但是,出于参考目的,
Mockito。(...)。Thenthrow(...)
用于返回值的方法调用。stream.mark(apcstream.available())
是一种方法调用, not 返回值(void return类型)。如::
在这种情况下,替换
为:
stream.mark(...)
will never throw any checked exceptions anyway so this is rather pointless.You also don't need to mark or reset the stream manually at all. The SDK does that behind the scenes.
If your stream is larger than 128 KB, then you may need to call
setReadLimit
to 1 byte greater than the size of the stream to avoid aResetException
when not using aFileInputStream
. If it isn't, then you don't need to do anything. Regardless, you don't need to mark or reset the stream yourself.However, for reference purposes,
Mockito.when(...).thenThrow(...)
is for method invocations that return a value.stream.mark(mockStream.available())
is a method invocation that does not return a value (void return type).As per docs:
In this case, replace:
With:
流
是bytearrayInputStream
。当 mark 。文档清楚地指出,可以抛出ioexception
,并且该参数被忽略。异常不是来自
标记
调用,它来自可用
调用。由于参数被忽略,只需将其替换为0,就无需捕获ioexception
。实际上,您会收到一个编译器错误:stream
is aByteArrayInputStream
. That will never ever ever throw any exception when mark is called. The documentation clearly states that noIOException
can be thrown, and that the argument is ignored.The exception doesn't come from the
mark
call, it comes from theavailable
call. As the argument is ignored, just replace it with 0, and you don't need to catch theIOException
. In fact, you'll get a compiler error: