У меня проблема с ошибкой моего модульного теста с этой ошибкой.
Error: java.lang.NullPointerException
com.amazon.heimdalltttingestionservicelambda.s3.S3FileTransmissionTest.mockS3FileRead(S3FileTransmissionTest.java:66)
com.amazon.heimdalltttingestionservicelambda.s3.S3FileTransmissionTest.testSuccess(S3FileTransmissionTest.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(Unknown Source)
public class S3FileTransmission {
AmazonS3Encryption s3EncryptionReceiveClient;
public void downloadDecryptWithS3Encryption(@NonNull final String fileKey, @NonNull final String bucketName,
@NonNull final File file)
{
try {
final InputStream downloadFileStream = this.s3EncryptionReceiveClient.getObject(bucketName,
fileKey).getObjectContent();
FileUtils.copyInputStreamToFile(downloadFileStream, file);
Environment.log("downloadDecryptWithS3Encryption:Completed file write to filepath: {} ", file.getPath());
} catch (final Exception e) {
Environment.log("[%s]: Error in downloading and decrypting filekey:[%s]",
LogMetricKeys.S3_FILE_DECRYPTION_FAILURE_, fileKey);
}
}
}
Код модульного теста:
@RunWith(MockitoJUnitRunner.class)
public class S3FileTransmissionTest extends BaseTester {
AmazonS3Encryption s3EncryptionReceiveClient;
@Mock
S3FileTransmission s3FileTransmission;
@Before
public void setup() {
super.setup();
s3EncryptionReceiveClient = Mockito.mock(AmazonS3Encryption.class);
}
@Test
public void testSuccess(){
File file = new File("/test/text.csv");
mockS3FileRead(prepareFileContent());
s3FileTransmission.downloadDecryptWithS3Encryption(BUCKET, KEY, file);
}
private String prepareFileContent() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(EmployeeLenelBadgeDataField.EXPECTED_HEADERS_STRING);
stringBuilder.append("\n");
for (int employeeIndex = 1; employeeIndex < 5; employeeIndex++) {
stringBuilder.append(MockDataRecords.getEmployeeLenelBadgeDataRecord());
stringBuilder.append("\n");
}
return stringBuilder.toString();
}
private void mockS3FileRead(String fileContent) {
// Inject test data
Answer<ObjectMetadata> answer = invocationOnMock -> {
File file = invocationOnMock.getArgument(1);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(fileContent);
fileWriter.flush();
fileWriter.close();
return null;
};
Mockito.when(s3EncryptionReceiveClient.getObject(anyString(), anyString()).getObjectContent()).thenAnswer(answer);
}
}