From eae9b5aa60b9761a683345a7f763905cc822cce8 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 11 Oct 2024 19:52:47 -0700 Subject: [PATCH] Upgrade Mockito from 1.x to 5.x --- pom.xml | 1 - .../jobConfigHistory/FileHistoryDaoTest.java | 7 +++++++ .../JobConfigBadgeActionTest.java | 7 +++---- .../JobConfigHistoryJobListenerTest.java | 8 ++++---- .../JobConfigHistoryProjectActionTest.java | 17 ++++++++++++----- .../JobConfigHistorySaveableListenerTest.java | 10 +++++----- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index f79eeec4..65746b46 100644 --- a/pom.xml +++ b/pom.xml @@ -84,7 +84,6 @@ org.mockito mockito-core - 1.10.19 test diff --git a/src/test/java/hudson/plugins/jobConfigHistory/FileHistoryDaoTest.java b/src/test/java/hudson/plugins/jobConfigHistory/FileHistoryDaoTest.java index fcb39680..49d9daf9 100644 --- a/src/test/java/hudson/plugins/jobConfigHistory/FileHistoryDaoTest.java +++ b/src/test/java/hudson/plugins/jobConfigHistory/FileHistoryDaoTest.java @@ -242,6 +242,7 @@ public void testGetRootDir() throws IOException { AtomicReference timestampHolder = new AtomicReference<>(); File result = sutWithoutUserAndDuplicateHistory.getRootDir(test1Config, timestampHolder); + when(mockedItem.getConfigFile()).thenCallRealMethod(); assertTrue(result.exists()); assertThat(result.getPath(), containsString("config-history" + File.separator + "jobs" + File.separator + "Test1")); @@ -256,6 +257,7 @@ public void testCreateNewItem() throws IOException { jobDir.mkdirs(); FileUtils.copyFile(test1Config.getFile(), new File(jobDir, "config.xml")); when(mockedItem.getRootDir()).thenReturn(jobDir); + when(mockedItem.getConfigFile()).thenCallRealMethod(); sutWithUserAndNoDuplicateHistory.createNewItem(mockedItem); assertTrue(new File(jobDir, "config.xml").exists()); final File historyDir = new File(jenkinsHome, @@ -270,6 +272,7 @@ public void testCreateNewItem() throws IOException { @Test public void testSaveItem_AbstractItem() { when(mockedItem.getRootDir()).thenReturn(test1JobDirectory); + when(mockedItem.getConfigFile()).thenCallRealMethod(); sutWithUserAndNoDuplicateHistory.saveItem(mockedItem.getConfigFile()); assertEquals(5, getHistoryLength()); } @@ -302,6 +305,7 @@ private int getHistoryLength() { @Test public void testDeleteItem() { when(mockedItem.getRootDir()).thenReturn(test1JobDirectory); + when(mockedItem.getConfigFile()).thenCallRealMethod(); sutWithUserAndNoDuplicateHistory.deleteItem(mockedItem); } @@ -315,6 +319,7 @@ public void testRenameItem() throws IOException { // Rename of Job is already done in Listener. FileUtils.touch(new File(newJobDir, "config.xml")); when(mockedItem.getRootDir()).thenReturn(newJobDir); + when(mockedItem.getConfigFile()).thenCallRealMethod(); when(mockedItem.getName()).thenReturn("Test1"); sutWithUserAndNoDuplicateHistory.renameItem(mockedItem, "Test1", "NewName"); @@ -364,6 +369,7 @@ public void testGetRevisionsForItemWithoutHistory() throws IOException { @Test public void testGetOldRevision_Item() throws IOException { when(mockedItem.getRootDir()).thenReturn(test1JobDirectory); + when(mockedItem.getConfigFile()).thenCallRealMethod(); String identifier = "2012-11-21_11-42-05"; final XmlFile result = sutWithUserAndNoDuplicateHistory .getOldRevision(mockedItem, identifier); @@ -441,6 +447,7 @@ private void testGetOldRevision(final XmlFile result) throws IOException { @Test public void testHasOldRevision_Item() { when(mockedItem.getRootDir()).thenReturn(test1JobDirectory); + when(mockedItem.getConfigFile()).thenCallRealMethod(); assertTrue(sutWithUserAndNoDuplicateHistory.hasOldRevision( mockedItem.getConfigFile(), "2012-11-21_11-42-05")); assertFalse(sutWithUserAndNoDuplicateHistory.hasOldRevision( diff --git a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigBadgeActionTest.java b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigBadgeActionTest.java index f593f210..e432f7ba 100644 --- a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigBadgeActionTest.java +++ b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigBadgeActionTest.java @@ -27,7 +27,6 @@ import hudson.model.Build; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; -import hudson.model.ItemGroup; import hudson.model.Job; import hudson.model.Project; import hudson.model.Run; @@ -66,9 +65,9 @@ public class JobConfigBadgeActionTest { public JenkinsRule jenkinsRule = new JenkinsRule(); public JobConfigBadgeActionTest() { - final ItemGroup mockedItemGroup = mock(ItemGroup.class); - when(mockedItemGroup.getUrl()).thenReturn("/jobs/"); - when(mockedProject.getParent()).thenReturn(mockedItemGroup); + final Jenkins mockedJenkins = mock(Jenkins.class); + when(mockedJenkins.getUrl()).thenReturn("/jobs/"); + when(mockedProject.getParent()).thenReturn(mockedJenkins); when(mockedProject.getShortUrl()).thenReturn("jobname"); when(mockedBuild.getProject()).thenReturn(mockedProject); } diff --git a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryJobListenerTest.java b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryJobListenerTest.java index 8fce2adc..59259264 100644 --- a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryJobListenerTest.java +++ b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryJobListenerTest.java @@ -6,7 +6,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; /** @@ -25,7 +25,7 @@ public class JobConfigHistoryJobListenerTest { public void testOnCreated() { Item item = createItem(); sut.onCreated(item); - verifyZeroInteractions(mockedConfigHistoryListenerHelper); + verifyNoInteractions(mockedConfigHistoryListenerHelper); } @Test @@ -39,7 +39,7 @@ public void testOnCreatedAbstractItem() { public void testOnRenamed() { Item item = createItem(); sut.onRenamed(item, "", ""); - verifyZeroInteractions(mockedConfigHistoryListenerHelper); + verifyNoInteractions(mockedConfigHistoryListenerHelper); } @Test @@ -54,7 +54,7 @@ public void testOnRenamedAbstractItemWithoutConfiguredHistoryRootDir() { public void testOnDeleted() { Item item = createItem(); sut.onDeleted(item); - verifyZeroInteractions(mockedConfigHistoryListenerHelper); + verifyNoInteractions(mockedConfigHistoryListenerHelper); } @Test diff --git a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryProjectActionTest.java b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryProjectActionTest.java index fee80eb3..607637bb 100644 --- a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryProjectActionTest.java +++ b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistoryProjectActionTest.java @@ -6,9 +6,9 @@ import hudson.model.AbstractItem; import hudson.model.AbstractProject; import hudson.model.FreeStyleProject; -import hudson.model.ItemGroup; import hudson.model.Project; import jenkins.model.AbstractTopLevelItem; +import jenkins.model.Jenkins; import org.apache.commons.io.FileUtils; import org.hamcrest.Matchers; import org.junit.Before; @@ -27,8 +27,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -52,9 +53,9 @@ public class JobConfigHistoryProjectActionTest { private HistoryDao historyDao; public JobConfigHistoryProjectActionTest() { - ItemGroup mockedItemGroup = mock(ItemGroup.class); - when(mockedItemGroup.getFullName()).thenReturn(""); - when(mockedProject.getParent()).thenReturn(mockedItemGroup); + Jenkins mockedJenkins = mock(Jenkins.class); + when(mockedJenkins.getFullName()).thenReturn(""); + when(mockedProject.getParent()).thenReturn(mockedJenkins); when(mockedProject.getFullName()).thenReturn("Test1"); } @@ -225,6 +226,7 @@ private List testJobXHasYHistoryEntries(final String jobDir, .thenReturn(true); when(mockedProject.getRootDir()) .thenReturn(testConfigs.getResource(jobDir)); + when(mockedProject.getConfigFile()).thenCallRealMethod(); final JobConfigHistoryProjectAction sut = createAction(); final List result = sut.getJobConfigs(); assertEquals(noOfHistoryEntries, result.size()); @@ -234,10 +236,12 @@ private List testJobXHasYHistoryEntries(final String jobDir, private List testJobXHasYHistoryEntries(final String jobDir, final int noOfHistoryEntries, int from, int to) { + reset(mockedProject); when(mockedProject.hasPermission(AbstractProject.CONFIGURE)) .thenReturn(true); when(mockedProject.getRootDir()) .thenReturn(testConfigs.getResource(jobDir)); + when(mockedProject.getConfigFile()).thenCallRealMethod(); final JobConfigHistoryProjectAction sut = createAction(); final List result = sut.getJobConfigs(from, to); assertEquals(noOfHistoryEntries, result.size()); @@ -250,6 +254,7 @@ public void testGetFile() throws Exception { .thenReturn(true); when(mockedProject.getRootDir()) .thenReturn(testConfigs.getResource("jobs/Test1")); + when(mockedProject.getConfigFile()).thenCallRealMethod(); when(mockedRequest.getParameter("timestamp")) .thenReturn("2012-11-21_11-40-28"); final JobConfigHistoryProjectAction sut = createAction(); @@ -340,6 +345,7 @@ private List prepareGetLines(final String timestamp1, .thenReturn(true); when(mockedProject.getRootDir()) .thenReturn(testConfigs.getResource("jobs/Test1")); + when(mockedProject.getConfigFile()).thenCallRealMethod(); JobConfigHistoryProjectAction sut = createAction(); return sut.getLines(); } @@ -352,6 +358,7 @@ public void testDoRestore() throws Exception { .thenReturn(true); when(mockedProject.getRootDir()) .thenReturn(testConfigs.getResource("jobs/Test1")); + when(mockedProject.getConfigFile()).thenCallRealMethod(); JobConfigHistoryProjectAction sut = createAction(); sut.doRestore(mockedRequest, mockedResponse); verify(mockedProject).save(); diff --git a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistorySaveableListenerTest.java b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistorySaveableListenerTest.java index d8e75971..2783484b 100644 --- a/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistorySaveableListenerTest.java +++ b/src/test/java/hudson/plugins/jobConfigHistory/JobConfigHistorySaveableListenerTest.java @@ -5,10 +5,10 @@ import hudson.model.Saveable; import org.junit.Test; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; /** @@ -29,16 +29,16 @@ public void testOnChangeNotSaveable() { .thenReturn(false); JobConfigHistorySaveableListener sut = new JobConfigHistorySaveableListenerImpl(); sut.onChange(null, null); - verifyZeroInteractions(mockedConfigHistoryListenerHelper); + verifyNoInteractions(mockedConfigHistoryListenerHelper); } @Test public void testOnChangeSaveable() { - when(mockedPlugin.isSaveable(any(Saveable.class), any(XmlFile.class))) + when(mockedPlugin.isSaveable(null, null)) .thenReturn(true); JobConfigHistorySaveableListener sut = new JobConfigHistorySaveableListenerImpl(); sut.onChange(null, null); - verify(mockedConfigHistoryListenerHelper).saveItem(any(XmlFile.class)); + verify(mockedConfigHistoryListenerHelper).saveItem(null); } private class JobConfigHistorySaveableListenerImpl