Joe*_*mes 5 java spring amazon-s3 powermockito
尝试使用PowerMock运行Mock测试时出现此堆栈
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
Run Code Online (Sandbox Code Playgroud)
我已经尝试过使用@添加@PowerMockIgnore的建议,org.apache.http.con.ssl.*但是这样做会导致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.*" })
Run Code Online (Sandbox Code Playgroud)
示例Bean:
@Configuration
@Profile("Test")
public class S3Configuration {
@Bean
public AmazonS3Client amazonS3Client() throws IOException {
return new AmazonS3Client(new EnvironmentVariableCredentialsProvider());
}
}
Run Code Online (Sandbox Code Playgroud)
我能够通过添加一个模拟 bean 并返回它的自定义配置文件来解决这个问题。
@Configuration
@Profile("Test")
public class TestConfig {
@Mock
AmazonS3Client client;
public TestConfig(){
MockitoAnnotations.initMocks(this);
}
@Bean
public AmazonS3Client amazonS3Client(){
return client;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5668 次 |
| 最近记录: |