have been struggling to mock a static method using mockito,
the method I am trying to mock is
CloudFrontUrlSigner.getSignedURLWithCannedPolicy
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/cloudfront/CloudFrontUrlSigner.html
When running the test it repeatedly fails to mock and enters the method, throwing an exception,
Not sure where I am going wrong to make the when clause match correctly,
any help is greatly appreciated
Mocked up a reduced version of the service:
package com.example.demo.service;
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.URL;
import java.security.PrivateKey;
@Service
public class CloudFrontSigningService {
// defined this way for simplicity
PrivateKey privateKey = new PrivateKey() {
@Override
public String getAlgorithm() {
return null;
}
@Override
public String getFormat() {
return null;
}
@Override
public byte[] getEncoded() {
return new byte[0];
}
};
public CloudFrontSigningService() {
}
public URL generateSignedURL(String pathToSign){
// String here for simplicity instead of Request with path, expirationSeconds etc
try {
Integer expirationSeconds = 20; // would be gotten from request passed in
String cloudFrontKeyPairId = "myKeyPairID";
String signedURL = CloudFrontUrlSigner.getSignedURLWithCannedPolicy(
pathToSign,
cloudFrontKeyPairId,
privateKey,
new DateTime(DateTimeZone.UTC).plusSeconds(expirationSeconds).toDate()
);
return new URL(signedURL);
} catch (IOException ex){
System.out.println("error generating signedURL : " + ex);
throw new Error("my error");
}
}
}
My attempts at testing the method so far
package com.example.demo.service;
import com.amazonaws.services.cloudfront.CloudFrontUrlSigner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.junit.Assert;
import java.io.IOException;
import java.net.URL;
import java.security.PrivateKey;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
class CloudFrontSigningServiceTest {
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
CloudFrontUrlSigner cloudFrontUrlSigner = mock(CloudFrontUrlSigner.class);
// inject some other mocks (removed for this example)
@InjectMocks
CloudFrontSigningService cloudFrontSigningService;
@Test
void testGenerateSignedURL_attempt1() {
String pathToSign = "s3://my-bucket-name/path/to/object.jpg";
Mockito.when(cloudFrontUrlSigner.getSignedURLWithCannedPolicy(
any(String.class),
any(String.class),
any(PrivateKey.class),
any(Date.class)
)
).thenReturn("http://example.cloudfront.net/path/to/object.jpg");
URL signedUrl = cloudFrontSigningService.generateSignedURL(pathToSign);
Assert.assertNotNull(signedUrl);
}
@Test
void testGenerateSignedURL_attempt2() throws IOException {
// Arrange
String pathToSign = "/some/path";
String cloudFrontKeyPairId = "myKeyPairID";
byte[] privateKey = "private_key_bytes".getBytes();
CloudFrontSigningService service = new CloudFrontSigningService();
CloudFrontUrlSigner signerMock = Mockito.spy(CloudFrontUrlSigner.class);
when(signerMock.getSignedURLWithCannedPolicy(
Mockito.eq(pathToSign),
Mockito.eq(cloudFrontKeyPairId),
Mockito.any(),
Mockito.any()
)).thenReturn("https://signed-url.example.com");
// Act
URL signedURL = service.generateSignedURL(pathToSign);
// Assert
assertNotNull(signedURL);
Mockito.verify(signerMock).getSignedURLWithCannedPolicy(
Mockito.eq(pathToSign),
Mockito.eq(cloudFrontKeyPairId),
Mockito.any(),
Mockito.any()
);
}
}
Dependencies I am using
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-cloudfront</artifactId>
<version>1.12.649</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
<version>5.11.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
<version>5.2.0</version>
</dependency>
gmarty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.