【问题标题】:Java - Skip line of mocked s3Client class from a junitJava - 从 junit 跳过模拟 s3Client 类的行
【发布时间】:2023-01-23 23:40:09
【问题描述】:

当从 junit 执行 myMethod 时,我需要能够跳过下面的 s3Client 行。 MyClass 没有被模拟,myMethod 也没有:

MyClass {
    myMethod(String bucketName, String path) {

        // do stuff

        // skip below when mocked in junit
        s3Client.deleteObject(new DeleteObjectRequest(bucketName, path));

        // more stuff
    {
{

在 junit 我有:

s3Client = mock(AmazonS3.class);
when(s3Client.deleteObject(any(DeleteObjectRequest.class))).thenReturn(null);

“when”不编译:

when(T) cannot be applied to void. reason: no instances of type variable T exist so that void conforms to T.

同样,我只需要从 junit 跳过这一行。任何解决方案表示赞赏。谢谢你。

【问题讨论】:

  • deleteObjects vs deleteObject 就像错误消息所说的那样?
  • @DaveNewton 感谢捕获。现在仍然错误但不同。更新帖子

标签: java mockito knox-amazon-s3-client


【解决方案1】:

这有效:

doNothing().when(s3Client).deleteObject(any(DeleteObjectRequest.class));

【讨论】:

    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 2018-03-09
    • 2014-07-27
    • 2019-11-19
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多