Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verifying of S3 putObject()-method does not work with Mockito #3368

Closed
johannes-link opened this issue Aug 22, 2022 · 2 comments
Closed

Verifying of S3 putObject()-method does not work with Mockito #3368

johannes-link opened this issue Aug 22, 2022 · 2 comments
Assignees

Comments

@johannes-link
Copy link

Describe the bug

When mocking software.amazon.awssdk.services.s3.S3Client with Mockito its not possible to verify PutObjectResponse putObject(PutObjectRequest putObjectRequest, RequestBody requestBody)-method.

Following error-message is appearing:
Caused by: Argument(s) are different! Wanted: s3Client.putObject( PutObjectRequest(Bucket=***, Key=***), software.amazon.awssdk.core.sync.RequestBody@2591f310 ); -> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) Actual invocations have different arguments: s3Client.putObject( PutObjectRequest(Bucket=***, Key=***), software.amazon.awssdk.core.sync.RequestBody@4bbddcc8 );

Expected Behavior

Verifying putObject()-method should run without erros when arguments are equal.

Current Behavior

An error occures despite arguments are equal.

Reproduction Steps

Example Test:

package com.demo;

import org.junit.Test;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class AwsS3Test {
    S3Client s3Client = mock(S3Client.class);

    @Test
    public void s3Client_test() {
        String bucketName = "bucket-name";
        String key = "key";
        PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(bucketName).key(key).build();
        RequestBody requestBody = RequestBody.fromString("file-content");

        s3Client.putObject(putObjectRequest, requestBody);

        verify(s3Client, times(1)).putObject(putObjectRequest, RequestBody.fromString("file-content"));
    }
}

This is a simple example. In my real code I had a class which is using S3Client to store files in S3. I tested a method of my class and verified putObject()-method from s3Client like in the example above.

Possible Solution

Implement equals()-method in software.amazon.awssdk.core.sync.RequestBody.

Additional Information/Context

I would like to work on this. You can assign it to me.

AWS Java SDK version used

2.17.73

JDK version used

14.0.1

Operating System and version

Windows 10

@johannes-link johannes-link added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 22, 2022
@yasminetalby yasminetalby self-assigned this Aug 23, 2022
@yasminetalby yasminetalby removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 30, 2022
@yasminetalby
Copy link

Hello @j-l1nk ,

Thank you very much for your submission.
We always welcome PR from the community on new features for the AWS Java SDK.
However, since RequestBody has an inputStream, we will end up reading the input stream every time while doing the Equals. Thus implementing equals in RequestBody might end up unnecessary reading of inputStreams and is not a solution we would consider in this case.

We recommend that you use the same requestBody instead of creating a new one while verifying.

Thank you very much for your collaboration.

Best,

Yasmine

@yasminetalby yasminetalby added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. and removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. labels Aug 30, 2022
@github-actions
Copy link

github-actions bot commented Sep 2, 2022

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants