Skip to content

Commit

Permalink
chore(test): migrate tests to Mockito 5 API IQSS#9782
Browse files Browse the repository at this point in the history
Replace the following API usages no longer available:

- Mockito.verifyZeroInteractions() does not exist anymore since Mockito 4
- Mockito.any() does no longer match varargs since Mockito 5
- org.mockito.Matcher does no longer exist since Mockito 4
  • Loading branch information
poikilotherm committed Aug 22, 2023
1 parent 9410880 commit 494624b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void updateMetadataBlockFacetsRoot_should_return_200_and_make_no_update_w
Response result = target.updateMetadataBlockFacetsRoot(containerRequestContext, VALID_DATAVERSE.getAlias(), "true");

MatcherAssert.assertThat(result.getStatus(), Matchers.is(200));
Mockito.verifyZeroInteractions(engineSvc);
Mockito.verifyNoInteractions(engineSvc);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Matchers;
import org.mockito.ArgumentMatchers;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -129,7 +130,7 @@ public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
//Mocking a query to return no results when
//checking for existing role in DB
TypedQuery mockedQuery = mock(TypedQuery.class);
when(mockedQuery.setParameter(Matchers.anyString(), Matchers.anyObject())).thenReturn(mockedQuery);
when(mockedQuery.setParameter(ArgumentMatchers.anyString(), ArgumentMatchers.any())).thenReturn(mockedQuery);
when(mockedQuery.getSingleResult()).thenReturn(null);
return mockedQuery;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void should_not_update_dataverse_when_root_value_does_not_change() throws

Mockito.verify(dataverse).isMetadataBlockFacetRoot();
Mockito.verifyNoMoreInteractions(dataverse);
Mockito.verifyZeroInteractions(context.dataverses());
Mockito.verifyNoInteractions(context.dataverses());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import edu.harvard.iq.dataverse.Dataset;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Matchers;

import jakarta.ejb.EJBException;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import org.mockito.ArgumentMatchers;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -36,15 +36,15 @@ void setup() {
@Test
void testGetDatasetMetricsByDatasetMonthCountry_withoutResults() {
when(query.getResultList()).thenReturn(new ArrayList());
when(this.serviceBean.em.createQuery(Matchers.anyString())).thenReturn(query);
when(this.serviceBean.em.createQuery(ArgumentMatchers.anyString())).thenReturn(query);

assertNull(serviceBean.getDatasetMetricsByDatasetMonthCountry(dataset, "01-01", "CH"));
}

@Test
void testGetDatasetMetricsByDatasetMonthCountry_throwsForMultipleResults() {
when(query.getResultList()).thenReturn(Arrays.asList(1, 2));
when(this.serviceBean.em.createQuery(Matchers.anyString())).thenReturn(query);
when(this.serviceBean.em.createQuery(ArgumentMatchers.anyString())).thenReturn(query);

assertThrows(EJBException.class, () -> {
serviceBean.getDatasetMetricsByDatasetMonthCountry(dataset, "01-01", "CH");
Expand All @@ -65,7 +65,7 @@ void testGetDatasetMetricsByDatasetMonthCountry_aggregatesForSingleResult() {
datasetMetrics.setDownloadsUniqueMachine(8L);

when(query.getResultList()).thenReturn(Arrays.asList(datasetMetrics));
when(this.serviceBean.em.createQuery(Matchers.anyString())).thenReturn(query);
when(this.serviceBean.em.createQuery(ArgumentMatchers.anyString())).thenReturn(query);

DatasetMetrics result = serviceBean.getDatasetMetricsByDatasetMonthCountry(dataset, "04.2019", "CH");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Matchers;

import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
Expand Down Expand Up @@ -97,7 +96,7 @@ void testAttemptPasswordReset_withNullNewPassword() {

@Test
void testAttemptPasswordReset_withValidationErrors() {
when(mockedPasswordValidatorServiceBean.validate(Matchers.anyString())).thenReturn(Arrays.asList("error"));
when(mockedPasswordValidatorServiceBean.validate(ArgumentMatchers.anyString())).thenReturn(Arrays.asList("error"));

PasswordChangeAttemptResponse passwordChangeAttemptResponse = passwordResetServiceBean.attemptPasswordReset(new BuiltinUser(), "newpass", "token");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void hasBagItPackage_should_return_false_when_bagit_file_not_found() {
boolean result = target.hasBagItPackage(fileDataProvider);

MatcherAssert.assertThat(result, Matchers.is(false));
Mockito.verifyZeroInteractions(manifestReader);
Mockito.verifyNoInteractions(manifestReader);
}

@Test
Expand Down Expand Up @@ -95,7 +95,7 @@ public void validateChecksums_should_return_error_when_no_bagit_file_in_data_pro
MatcherAssert.assertThat(result.getErrorMessage().isEmpty(), Matchers.is(false));
Mockito.verify(target).getMessage(Mockito.eq("bagit.validation.bag.file.not.found"), Mockito.any());

Mockito.verifyZeroInteractions(manifestReader);
Mockito.verifyNoInteractions(manifestReader);
}

@Test
Expand All @@ -119,7 +119,7 @@ public void validateChecksums_should_return_error_when_manifest_reader_returns_e

MatcherAssert.assertThat(result.success(), Matchers.is(false));
MatcherAssert.assertThat(result.getErrorMessage().isEmpty(), Matchers.is(false));
Mockito.verify(target).getMessage(Mockito.eq("bagit.validation.manifest.not.supported"), Mockito.any());
Mockito.verify(target).getMessage(Mockito.eq("bagit.validation.manifest.not.supported"), Mockito.any(Object[].class));

Mockito.verify(manifestReader).getManifestChecksums(fileDataProvider, expectedBagRoot);
}
Expand All @@ -140,7 +140,7 @@ public void validateChecksums_should_return_error_when_data_provider_do_not_have
for(Path filePath: checksums.getFileChecksums().keySet()) {
MatcherAssert.assertThat(result.getFileResults().get(filePath).isError(), Matchers.is(true));
}
Mockito.verify(target, Mockito.times(checksums.getFileChecksums().size())).getMessage(Mockito.eq("bagit.validation.file.not.found"), Mockito.any());
Mockito.verify(target, Mockito.times(checksums.getFileChecksums().size())).getMessage(Mockito.eq("bagit.validation.file.not.found"), Mockito.any(Object[].class));

Mockito.verify(manifestReader).getManifestChecksums(fileDataProvider, expectedBagRoot);
Mockito.verify(fileDataProvider).getFilePaths();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void handleBagItPackage_should_return_error_when_no_files_in_data_provide
createDataFileResultAsserts(result);

handleBagItPackageAsserts(fileDataProvider);
Mockito.verifyZeroInteractions(postProcessor);
Mockito.verifyNoInteractions(postProcessor);
}

@Test
Expand Down Expand Up @@ -231,7 +231,7 @@ public void handleBagItPackage_should_return_error_when_FileExceedsMaxSizeExcept

handleBagItPackageAsserts(dataProviderSpy);
createDataFileAsserts(Arrays.asList(Path.of(bagEntry)), 2);
Mockito.verifyZeroInteractions(postProcessor);
Mockito.verifyNoInteractions(postProcessor);
}

@Test
Expand All @@ -249,7 +249,7 @@ public void handleBagItPackage_should_return_error_when_the_maximum_number_of_fi
MatcherAssert.assertThat(result.getErrors().size(), Matchers.is(1));

handleBagItPackageAsserts(dataProviderSpy);
Mockito.verifyZeroInteractions(postProcessor);
Mockito.verifyNoInteractions(postProcessor);
}

@Test
Expand All @@ -266,7 +266,7 @@ public void handleBagItPackage_should_return_error_when_bag_validation_fails() t

handleBagItPackageAsserts(dataProviderSpy);
createDataFileAsserts(dataProviderWithDataFiles.dataProvider.getFilePaths());
Mockito.verifyZeroInteractions(postProcessor);
Mockito.verifyNoInteractions(postProcessor);
}

private void createDataFileResultAsserts(CreateDataFileResult result) {
Expand Down

0 comments on commit 494624b

Please sign in to comment.