我正在从 S3 存储桶中读取一个文件,如下所示:
class VersionServiceImpl implements VersionService {
VersionDto versiondto = new VersionDto();
try {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(versionConfig.getAccessKeyId(), versionConfig.getSecretAccessKey());
AmazonS3 s3Client=new AmazonS3Client(awsCreds);
S3Object s3object = s3Client.getObject(new GetObjectRequest(versionConfig.getBucketKey(), versionConfig.getFileNameKey()));
BufferedReader reader = new BufferedReader(new InputStreamReader(s3object.getObjectContent()));
while (true) {
String line = reader.readLine();
//System.out.println(" "+line);
if (line == null)
break;
String[] fields=line.split("=");
switch (fields[0]) {
case "MAJOR":
versiondto.setMajor(fields[1] != null ? fields[1] : "0");
break;
case "MINOR":
versiondto.setMinor(fields[1] != null ? fields[1] : "0");
break;
case "HOTFIXPATCH":
versiondto.setHotFixPatch(fields[1] != null ? fields[1] : "0");
break;
default:
LOGGER.info("INVALID/EXTRA FIELD IN VERSION.TXT FILE", fields[0]);
}
}
s3object.close();
} catch (AmazonServiceException ase) {
LOGGER.error("Caught an AmazonServiceException from GET requests", ase);
} catch (AmazonClientException ace) {
LOGGER.error("Caught an AmazonClientException", ace);
} catch (IOException ioe) {
LOGGER.error("IOE Exception reading file from S3", ioe);
}
return versiondto;
}
我正在尝试模拟 AWS 类并针对此方法运行 jUnit 测试,但几乎没有异常。我曾尝试使用 mockito.spy,模拟 aws 类。但在测试读取文件方法时仍然发现异常以下是 JUnit 测试:
public class VersionServiceTest {
@Mock
S3ObjectInputStream s3ObjectInputStream;
@InjectMocks
private VersionServiceImpl roleService;
@Mock
private VersionConfig versionConfig;
@Mock
private S3Object s3Object ;
@Mock
BasicAWSCredentials awsCreds;
@Mock
private AmazonS3 s3Client;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
when(versionConfig.getAccessKeyId()).thenReturn("AKIAJ6HT2QIXO452VW4A");
when(versionConfig.getBucketKey()).thenReturn("version-test-ops/version");
when(versionConfig.getFileNameKey()).thenReturn("version.txt");
when(versionConfig.getSecretAccessKey()).thenReturn("6f+qJ/i0tQprEftE+pwa0CmnjTtdzoOYnuG0DGbN");
awsCreds= new BasicAWSCredentials(versionConfig.getAccessKeyId(), versionConfig.getSecretAccessKey());
}
@Test
public void TestGetVersion() throws IOException {
String bucket = "version-test-ops/version";
String keyName = "version.txt";
s3Object.setBucketName(bucket);
s3Object.setKey(keyName);
when(any(BasicAWSCredentials.class)).thenReturn(awsCreds);
when(any(AmazonS3Client.class)).thenReturn((AmazonS3Client) s3Client);
when(s3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3Object);
when(s3Object.getObjectContent()).thenReturn(s3ObjectInputStream);
BufferedReader reader = Mockito.mock(BufferedReader.class);
Mockito.when(reader.readLine()).thenReturn("ENV=DEV3", "MAJOR=0", "MINOR=0", "CRSPNG_VERSION_HOTFIXPATCH=384",
"DATEOFDEPLOY=15-12-2017", "SPRINT=30");
VersionDto dto= roleService.getVersionDetails();
}
}
以下是例外:
java.lang.LinkageError: loader constraint violation: when resolving overridden method "com.amazonaws.http.conn.ssl.SdkTLSSocketFactory.connectSocket(ILjava/net/Socket;Lorg/apache/http/HttpHost;Ljava/net/InetSocketAddress;Ljava/net/InetSocketAddress;Lorg/apache/http/protocol/HttpContext;)Ljava/net/Socket;" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, com/amazonaws/http/conn/ssl/SdkTLSSocketFactory, and its superclass loader (instance of sun/misc/Launcher$AppClassLoader), have different Class objects for the type org/apache/http/protocol/HttpContext used in the signature
at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.getPreferredSocketFactory(ApacheConnectionManagerFactory.java:87)
at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:65)
at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:58)
at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:50)
at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:38)
at com.amazonaws.http.AmazonHttpClient.<init>(AmazonHttpClient.java:260)
at com.amazonaws.AmazonWebServiceClient.<init>(AmazonWebServiceClient.java:160)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:519)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:499)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:481)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:453)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:435)
at com.VersionServiceImpl.getVersionDetails(VersionServiceImpl.java:38)