简体   繁体   English

NullPointerException 而 mocking s3Service + junit

[英]NullPointerException while mocking s3Service + junit

I am using amazon s3 for the document storage.我正在使用 amazon s3 进行文档存储。 I need to write test cases for the service class which is using s3.我需要为使用 s3 的服务 class 编写测试用例。

How to mock the s3 object?如何模拟 s3 object?

I have tried the below, but getting the NPE.我试过下面的方法,但得到了 NPE。

sample test case:示例测试用例:

@ExtendWith(MockitoExtension.class)
class MyServiceTest {

    @InjectMocks
    private MyService myService;

    @InjectMocks
    private S3Service s3Service;
    @Mock
    private S3Configuration.S3Storage s3Storage;
    @Mock
    private AmazonS3 amazonS3;

    @BeforeEach
    public void setup() {
        ReflectionTestUtils.setField(myService, "s3Service", s3Service);
        ReflectionTestUtils.setField(s3Service, "s3Storage", s3Storage);
    }

 @Test
    void getDetailTest() {

        given(s3Storage.getClient()).willReturn(amazonS3);
        given(s3Service.getBucket()).willReturn("BUCKET1");
        given(s3Service.readFromS3(s3Service.getBucket(),"myfile1.txt")).willReturn("hello from s3 file"); //Null pointer exception
}
}

Below is the s3 service class sample where it is throwing NPE.下面是 s3 服务 class 抛出 NPE 的示例。

public String readFromS3(String bucketName, String key) {
        var s3object = s3Storage.getClient().getObject(new GetObjectRequest(bucketName, key));
//s3Object is getting null when run the test case.
    //more logic

}

How to mock s3Service.readFromS3() from MyService class?如何从MyService class 模拟s3Service.readFromS3()

You don't need to mock each object in your test, it's quite fine to construct your service using mocks...您不需要在测试中模拟每个 object,使用模拟构建您的服务非常好......

@ExtendWith(MockitoExtension.class)
class MyServiceTest {

    private MyService myService;

    @Mock
    private AmazonS3 amazonS3;

    @BeforeEach
    public void setup() {
        this.myService = new MyService(amazonS3);
    }

    @Test
    void getDetailTest() {
        given(this.amazonS3.getXXX()).willReturn(new Yyyyy());
        assertEqual("ok", this.myService.doSmth());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Junit 中模拟 S3AsyncClient - How to mock S3AsyncClient in Junit 如何在 java Junit 中模拟 S3AsyncClient - How to mock S3AsyncClient in java Junit 获取 NullPointerException:堆栈:java.lang.reflect.InvocationTargetException 同时在 java 中执行 Azure function 应用程序 - Getting NullPointerException: Stack: java.lang.reflect.InvocationTargetException while executing a Azure function app in java PlatformException(PlatformException(通道错误,无法在通道上建立连接。,null,null))而使用 Firestore 的 mocking 方法 - PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null, null)) while mocking method that use firestore 更改 AWS ECS 服务的安全组 - Change AWS ECS service's security groups 云运行服务日志的摘要不是 textpayload - cloud run service log's summary is not textpayload IAM S3 服务角色用例 - IAM S3 Service Role Use Case NullPointerException: 在 firebase - NullPointerException: in firebase gcloud run service replace 删除以前的修订前缀 - gcloud run service replace removes previous revision(s) prefix(s) 使用玩笑模拟 aws-sdk S3#putObject 实例方法 - Mocking aws-sdk S3#putObject instance method using jest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM