Search code examples
javaspringamazon-s3powermockito

PowerMock AmazonS3Client Config Issue


I'm getting this stack when trying to run a Mock test using PowerMock

 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.s3.AmazonS3Client]: Factory method   'amazonS3Client' threw exception; nested exception is org.apache.http.conn.ssl.SSLInitializationException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
 Caused by: org.apache.http.conn.ssl.SSLInitializationException: class  configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
 Caused by: java.security.NoSuchAlgorithmException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext

I have tried suggestions of adding an @PowerMockIgnore with the org.apache.http.con.ssl.* but doing that causes my Rabbit connector to fail. I wasn't sure if there are any suggestions have both load for my test. Or not initialize one if it is not needed for the test?

I am limited in what I can provide since this is something for my company.

Using Amazon SDK: 1.11.69

Here is how I'm configuring my tests

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath:applicationContext-test.xml"})
@TestExecutionListeners(listeners={ServletTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class,
        WithSecurityContextTestExecutionListener.class})
@PrepareForTest({Observable.class,HardDeleteUserCommand.class,SoftDeleteUserCommand.class})
@PowerMockIgnore({ "javax.management.*", "ch.qos.logback.*",
    "org.slf4j.*" })

Example Bean:

@Configuration
@Profile("Test")
public class S3Configuration {
    @Bean
    public AmazonS3Client amazonS3Client() throws IOException {
          return new AmazonS3Client(new EnvironmentVariableCredentialsProvider());
    }
}

Solution

  • I was able to solve this by adding a custom Configuration file that mocks the bean and returns it.

    @Configuration
    @Profile("Test")
    public class TestConfig {
    
        @Mock
        AmazonS3Client client;
    
        public TestConfig(){
            MockitoAnnotations.initMocks(this);
        }
    
        @Bean
        public AmazonS3Client amazonS3Client(){
            return client;
        }
    }