我想模拟一个扩展InputStream的专有类,模拟读取,验证关闭



我想使用Mockito来模拟AmazonS3并测试从中打开流然后在我的代码读取后验证流是否关闭。我还想从流中读取字节。类似这样的东西:

AmazonS3 client = mock(AmazonS3.class);
when(tm.getAmazonS3Client()).thenReturn(client);
S3Object response = mock(S3Object.class); 
when(client.getObject(any(GetObjectRequest.class))).thenReturn(response);
S3ObjectInputStream stream = mock(S3ObjectInputStream.class); 
when(response.getObjectContent()).thenReturn(stream);

以某种方式模拟读取方法

MyObject me = new MyObject(client);
byte[] bra me.getBytes(File f, offset, length);
assertEquals(length, bra.length);
verify(stream).close();

您可以使用Mockito的Answer来模拟流。

    String expectedContents = "Some contents";
    InputStream testInputStream = new StringInputStream(expectedContents);
    S3ObjectInputStream s3ObjectInputStream = mock(S3ObjectInputStream.class);
    S3Object s3Object = mock(S3Object.class);
    AmazonS3Client amazonS3Client = mock(AmazonS3Client.class);
    S3AttachmentsService service = new S3AttachmentsService(amazonS3Client);
    when(s3ObjectInputStream.read(any(byte[].class))).thenAnswer(invocation -> {
        return testInputStream.read(invocation.getArgument(0));
    });

我这里有一个更广泛的例子。希望能有所帮助。

您可能可以通过一种简单的方式实现这一点:

when(stream.read()).thenReturn(0, 1, 2, 3 /* ... */);

也就是说,现在,你在嘲笑亚马逊的实现。这意味着,如果任何一个方法变成final,你的状态就会很糟糕,因为Mockito由于编译器的限制而不支持模拟final方法。嘲笑你不喜欢的类型很诱人,但可能会导致崩溃。

如果您的目标是测试getBytes是否返回正确的值并关闭其流,那么更稳定的方法可能是重构以使用任意InputStream:

class MyObject {
  public byte[] getBytes(File f, int offset, int length) {
    /* ... */
    // Delegate the actual call to a getBytes method.
    return getBytes(s3ObjectInputStream, f, offset, length);
  }
  /** Call this package-private delegate in tests with any arbitrary stream. */
  static byte[] getBytes(InputStream s, File f, int offset, int length) {
    /* ... */
  }
}

这时,您可以使用spy(new ByteArrayInputStream(YOUR_BYTE_ARRAY))对其进行测试,并获得一个非常有说服力的测试——通过调用verify(stream).close()来完成。

沿着这些路线,另一个解决方案是添加一个可以控制的接缝,有效地从远处包裹getBytes

class MyObject {
  public byte[] getBytes(File f, int offset, int length) {
    /* ... */
    InputStream inputStream = getStream(response.getObjectContent());
    /* ... */
  }
  /** By default, just pass in the stream you already have. */
  InputStream getStream(S3ObjectInputStream s3Stream) {
    return s3Stream;
  }
}
class MyObjectTest {
  @Test public void yourTest() {
    /* ... */
    MyObject myObject = new MyObject(client) {
      /** Instead of returning the S3 stream, insert your own. */
      @Override InputStream getStream() { return yourMockStream; }
    }
    /* ... */
  }
}

不过,请记住,您正在测试您认为AmazonS3应该工作的方式,而不是它是否在实践中继续工作。如果您的目标是"测试从[S3]打开流",那么针对实际S3实例运行的集成测试可能是一个好主意,以弥补S3 mock和S3之间的差距。

内部S3ObjectInputStream是对InputStream的抽象。所以在我看来,你也可以嘲笑我。代码看起来像这个

String expected = "This is a dummy mocked content";
InputStream inputStream = IOUtils.toInputStream(expected);
S3ObjectInputStream s3Input = new S3ObjectInputStream(inputStream,null);
S3Object s3Object = mock(S3Object.class);
AmazonS3Client amazonS3Client = mock(AmazonS3Client.class);
S3AttachmentsService service = new S3AttachmentsService(amazonS3Client);
when(amazonS3Client.getObject(any(GetObjectRequest.class)).thenReturn(s3Object);
when(s3Object.getObjectContent).thenReturn(s3Input);

最新更新