I have a unit test to test uploading a file to GCP storage. Here is the code for upload the file.
@Override
public boolean upload(StorageConfiguration storageConfiguration, File file) throws MetaFeedException {
// get google storage connection.
Optional<Storage> storage = getStorageConnection(storageConfiguration.getJsonCredentialFilePath());
// if GCP storage is empty, return empty.
if (!storage.isPresent()) {
throw new MetaFeedException("Failed to establish connection with GCP storage");
}
// upload blob with given file content
BlobId blobId = BlobId.of(storageConfiguration.getBucketName(),
storageConfiguration.getBucketPath().concat(file.getName()));
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(storageConfiguration.getContentType()).build();
try {
return storage.get().create(blobInfo, Files.readAllBytes(Paths.get(file.getAbsolutePath()))).exists();
} catch (Exception e) {
throw new MetaFeedException("Error occurred while uploading file.", e);
}
}
In my unit tests, I've done something like this,
@Test
public void should_upload_file_to_gcp_with_given_data() throws Exception {
File tempFile = File.createTempFile("file-name", "1");
StorageConfiguration storageConfiguration = new StorageConfiguration();
storageConfiguration.setBucketName("sample-bucket");
storageConfiguration.setBucketPath("ff/");
storageConfiguration.setJsonCredentialFilePath("json-credentials");
storageConfiguration.setContentType("text/plain");
StorageOptions defaultInstance = mock(StorageOptions.class);
Storage mockStorage = spy(Storage.class);
when(defaultInstance.getService()).thenReturn(mockStorage);
BlobId blobId = BlobId.of(storageConfiguration.getBucketName(), storageConfiguration.getBucketPath().concat(tempFile.getName()));
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(storageConfiguration.getContentType()).build();
doNothing().when(mockStorage).create(blobInfo, Files.readAllBytes(Paths.get(tempFile.getAbsolutePath())));
boolean upload = gcpStorageManager.upload(storageConfiguration, tempFile);
Assert.assertTrue(upload);
}
What I'm trying to do is prevent calling the create()
method. My point is I don't want to perform a real upload to GCP since it's a test. So I tried as above. But I got an error,
org.mockito.exceptions.base.MockitoException:
Only void methods can doNothing()!
Example of correct use of doNothing():
doNothing().
doThrow(new RuntimeException())
.when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception
the 2nd time is called
UPDATE
Optional<Storage> storage;
try {
//connect with the json key file if the key path is not empty
if (StringUtils.isNotEmpty(jsonCredentialFilePath) && Files.exists(Paths.get(jsonCredentialFilePath))) {
storage = Optional.ofNullable(StorageOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(jsonCredentialFilePath)))
.build().getService());
} else {
// if no json key file provided connect to storage without key file.
storage = Optional.ofNullable(StorageOptions.getDefaultInstance().getService());
}
} catch (Exception e) {
throw new MetaFeedException("Error occurred while connecting to GCP storage", e);
}
return storage;
Is there a way to fix this test for uploading file to GCP?