PowerMock AmazonS3Client 配置问题

8
当我尝试使用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

我尝试了添加一个带有org.apache.http.con.ssl.*的@PowerMockIgnore,但这样做会导致我的Rabbit连接器失败。我不确定是否有任何建议可以同时为我的测试加载两个内容。或者,如果测试不需要一个,则不初始化一个?
由于这是我公司的一些东西,我所能提供的内容有限。
使用Amazon SDK:1.11.69
以下是我如何配置我的测试。
@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.*" })

示例 Bean:

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

请提供一个 MCVE。 - Compass
我受限于我能提供的内容,因为这是我公司的事情。 - Joel Holmes
9
当我添加了 @PowerMockIgnore({ "javax.net.ssl.*" }) 后,这个错误就消失了。 - srkavin
2个回答

6

正如评论中的@srkavin所说,当我添加了@PowerMockIgnore({ "javax.net.ssl.*" })时,这个错误就消失了。


3

我通过添加自定义配置文件来模拟该 Bean 并返回它,从而解决了这个问题。

@Configuration
@Profile("Test")
public class TestConfig {

    @Mock
    AmazonS3Client client;

    public TestConfig(){
        MockitoAnnotations.initMocks(this);
    }

    @Bean
    public AmazonS3Client amazonS3Client(){
        return client;
    }
}

1
你在测试中如何使用配置文件? - KumarAnkit
我尝试模拟它,但实际实现仍然抛出错误,顺便说一下,当创建AWS Cognito SDK时,我也遇到了类似的错误。 - Abdeali Chandanwala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接