Skip to content

Commit

Permalink
replace deprecated MockitoAnnotations.initMocks with MockitoAnnotatio…
Browse files Browse the repository at this point in the history
…ns.openMocks (#66)
  • Loading branch information
krwong committed Sep 11, 2023
1 parent 18a4314 commit 9e65c72
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.unc.lib.boxc.migration.cdm.test.CdmEnvironmentHelper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -28,13 +29,14 @@
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

/**
* @author bbpennel
*/
public class CdmExportServiceTest {
private static final String PROJECT_NAME = "proj";
private AutoCloseable closeable;
@TempDir
public Path tmpFolder;

Expand All @@ -50,7 +52,7 @@ public class CdmExportServiceTest {

@BeforeEach
public void setup() throws Exception {
initMocks(this);
closeable = openMocks(this);
project = MigrationProjectFactory.createMigrationProject(
tmpFolder, PROJECT_NAME, null, "user", CdmEnvironmentHelper.DEFAULT_ENV_ID);
fieldService = new CdmFieldService();
Expand All @@ -77,6 +79,11 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
}).when(cdmFileRetrievalService).downloadDescAllFile();
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

private CdmExportOptions makeExportOptions() {
CdmExportOptions options = new CdmExportOptions();
options.setCdmUsername("user");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
Expand All @@ -22,6 +21,7 @@
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -40,6 +40,7 @@
public class CdmFieldServiceTest {
private static final String CDM_BASE_URL = "http://example.com:88/";
private static final String PROJECT_NAME = "proj";
private AutoCloseable closeable;
@TempDir
public Path tmpFolder;

Expand All @@ -54,7 +55,7 @@ public class CdmFieldServiceTest {

@BeforeEach
public void setup() throws Exception {
initMocks(this);
closeable = openMocks(this);
project = MigrationProjectFactory.createMigrationProject(
tmpFolder, PROJECT_NAME, null, "user", CdmEnvironmentHelper.DEFAULT_ENV_ID);
service = new CdmFieldService();
Expand All @@ -65,6 +66,11 @@ public void setup() throws Exception {
when(httpResp.getEntity()).thenReturn(respEntity);
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void retrieveCdmFieldsTest() throws Exception {
when(respEntity.getContent()).thenReturn(this.getClass().getResourceAsStream("/cdm_fields_resp.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.unc.lib.boxc.migration.cdm.test.BxcEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.CdmEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.SipServiceHelper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -13,7 +14,7 @@

import static edu.unc.lib.boxc.migration.cdm.test.PostMigrationReportTestHelper.assertContainsRow;
import static edu.unc.lib.boxc.migration.cdm.test.PostMigrationReportTestHelper.parseReport;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

/**
* @author bbpennel
Expand All @@ -26,6 +27,7 @@ public class PostMigrationReportServiceTest {
private static final String BOXC_URL_1 = BOXC_BASE_URL + BOXC_ID_1;
private static final String BOXC_URL_2 = BOXC_BASE_URL + BOXC_ID_2;
private static final String BOXC_URL_3 = BOXC_BASE_URL + BOXC_ID_3;
private AutoCloseable closeable;
@TempDir
public Path tmpFolder;

Expand All @@ -36,7 +38,7 @@ public class PostMigrationReportServiceTest {

@BeforeEach
public void setup() throws Exception {
initMocks(this);
closeable = openMocks(this);
project = MigrationProjectFactory.createMigrationProject(
tmpFolder, "proj", null, "user",
CdmEnvironmentHelper.DEFAULT_ENV_ID, BxcEnvironmentHelper.DEFAULT_ENV_ID);
Expand All @@ -50,6 +52,11 @@ public void setup() throws Exception {
service.init();
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void addSingleItemTest() throws Exception {
testHelper.populateDescriptions("gilmer_mods1.xml");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package edu.unc.lib.boxc.migration.cdm.services;

import edu.unc.lib.boxc.migration.cdm.exceptions.InvalidProjectStateException;
import edu.unc.lib.boxc.migration.cdm.exceptions.MigrationException;
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.test.BxcEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.CdmEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.SipServiceHelper;
import edu.unc.lib.boxc.migration.cdm.util.PostMigrationReportConstants;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -29,7 +28,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;

/**
* @author bbpennel
Expand All @@ -39,6 +38,7 @@ public class PostMigrationReportVerifierTest {
private static final String BOXC_URL_2 = "http://example.com/bxc/91c08272-260f-40f1-bb7c-78854d504368";
private static final String CDM_URL_1 = "http://localhost/cdm/singleitem/collection/proj/id/25";
private static final String CDM_URL_2 = "http://localhost/cdm/singleitem/collection/proj/id/26";
private AutoCloseable closeable;
@TempDir
public Path tmpFolder;
@Mock
Expand All @@ -50,7 +50,7 @@ public class PostMigrationReportVerifierTest {

@BeforeEach
public void setup() throws Exception {
initMocks(this);
closeable = openMocks(this);
project = MigrationProjectFactory.createMigrationProject(
tmpFolder, "proj", null, "user",
CdmEnvironmentHelper.DEFAULT_ENV_ID, BxcEnvironmentHelper.DEFAULT_ENV_ID);
Expand All @@ -63,6 +63,11 @@ public void setup() throws Exception {
verifier.setHttpClient(httpClient);
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

@Test
public void noReportTest() throws Exception {
Assertions.assertThrows(InvalidProjectStateException.class, () -> {
Expand Down

0 comments on commit 9e65c72

Please sign in to comment.