Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
    }
}
like image 699
Joel Holmes Avatar asked Dec 21 '16 20:12

Joel Holmes


2 Answers

As @srkavin said in the comments, this error went away when I added @PowerMockIgnore({ "javax.net.ssl.*" })

like image 144
Maria Ines Parnisari Avatar answered Oct 25 '22 00:10

Maria Ines Parnisari


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;
    }
}
like image 43
Joel Holmes Avatar answered Oct 25 '22 01:10

Joel Holmes