Как написать контрольный пример для метода, имеющего FileUtils.copyInputStreamToFile - PullRequest
0 голосов
/ 27 апреля 2020

Я написал тестовый пример ниже, чтобы проверить метод, имеющий FileUtils.copyInputStreamToFile, но он не работает и становится ниже ошибки

null java .lang.NullPointerException com.amazon.heimdalltttingestionservicelambda.s3. S3FileTransmissionTest.testSuccess (S3FileTransmissionTest. java: 52)

код модульного теста:

@RunWith(MockitoJUnitRunner.class)
public class S3FileTransmissionTest extends BaseTester {

@Mock
AmazonS3Encryption s3EncryptionReceiveClient;
S3FileTransmission s3FileTransmission;

@Mock
private File FILE;

@Mock
FileUtils fileUtils;
@Mock
AmazonS3 amazonS3;

@Before
public void setup() {
    super.setup();

    s3FileTransmission = testServiceComponent.s3FileTransmission();
}

@Test
public void testSuccess(){
    S3ObjectInputStream s3ObjectInputStream = Mockito.mock(S3ObjectInputStream.class);
    Mockito.when(s3EncryptionReceiveClient.getObject(BUCKET, KEY).getObjectContent()).thenReturn(null);
    try{
        s3FileTransmission.downloadDecryptWithS3Encryption(BUCKET, KEY, FILE);
    } catch (Exception exception){

    }
}

Код для проверки:

public class S3FileTransmission {

AmazonS3Encryption s3EncryptionReceiveClient;

/**
 * This S3 operation is specifically meant for download decrypt file
 * to filepath in local.
 * @param fileKey s3 file key
 * @param bucketName s3 bucket name
 * @param file local file
 */
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);

    }
}
...