Skip to content

Commit

Permalink
Upgrade Mockito from 1.x to 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Oct 12, 2024
1 parent b9a0808 commit eae9b5a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public void testGetRootDir() throws IOException {
AtomicReference<Calendar> 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"));
Expand All @@ -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,
Expand All @@ -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());
}
Expand Down Expand Up @@ -302,6 +305,7 @@ private int getHistoryLength() {
@Test
public void testDeleteItem() {
when(mockedItem.getRootDir()).thenReturn(test1JobDirectory);
when(mockedItem.getConfigFile()).thenCallRealMethod();
sutWithUserAndNoDuplicateHistory.deleteItem(mockedItem);
}

Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -25,7 +25,7 @@ public class JobConfigHistoryJobListenerTest {
public void testOnCreated() {
Item item = createItem();
sut.onCreated(item);
verifyZeroInteractions(mockedConfigHistoryListenerHelper);
verifyNoInteractions(mockedConfigHistoryListenerHelper);
}

@Test
Expand All @@ -39,7 +39,7 @@ public void testOnCreatedAbstractItem() {
public void testOnRenamed() {
Item item = createItem();
sut.onRenamed(item, "", "");
verifyZeroInteractions(mockedConfigHistoryListenerHelper);
verifyNoInteractions(mockedConfigHistoryListenerHelper);
}

@Test
Expand All @@ -54,7 +54,7 @@ public void testOnRenamedAbstractItemWithoutConfiguredHistoryRootDir() {
public void testOnDeleted() {
Item item = createItem();
sut.onDeleted(item);
verifyZeroInteractions(mockedConfigHistoryListenerHelper);
verifyNoInteractions(mockedConfigHistoryListenerHelper);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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");
}

Expand Down Expand Up @@ -225,6 +226,7 @@ private List<ConfigInfo> testJobXHasYHistoryEntries(final String jobDir,
.thenReturn(true);
when(mockedProject.getRootDir())
.thenReturn(testConfigs.getResource(jobDir));
when(mockedProject.getConfigFile()).thenCallRealMethod();
final JobConfigHistoryProjectAction sut = createAction();
final List<ConfigInfo> result = sut.getJobConfigs();
assertEquals(noOfHistoryEntries, result.size());
Expand All @@ -234,10 +236,12 @@ private List<ConfigInfo> testJobXHasYHistoryEntries(final String jobDir,
private List<ConfigInfo> 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<ConfigInfo> result = sut.getJobConfigs(from, to);
assertEquals(noOfHistoryEntries, result.size());
Expand All @@ -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();
Expand Down Expand Up @@ -340,6 +345,7 @@ private List<SideBySideView.Line> prepareGetLines(final String timestamp1,
.thenReturn(true);
when(mockedProject.getRootDir())
.thenReturn(testConfigs.getResource("jobs/Test1"));
when(mockedProject.getConfigFile()).thenCallRealMethod();
JobConfigHistoryProjectAction sut = createAction();
return sut.getLines();
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down

0 comments on commit eae9b5a

Please sign in to comment.