Mocking static method in Enum class with Mockito

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>

New contributor

gmarty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật