From cff50871022589e219ee218f97ebb000d2a01abf Mon Sep 17 00:00:00 2001 From: Arnab Munshi <119315596+code-arnab@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:48:48 +0530 Subject: [PATCH] [JENKINS-70560] Improve DescriptorImpl test coverage (#285) * [Jenkins-70560] Improve DescriptorImpl test coverage * Update --- .../versioncolumn/VersionMonitorTest.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/test/java/hudson/plugin/versioncolumn/VersionMonitorTest.java b/src/test/java/hudson/plugin/versioncolumn/VersionMonitorTest.java index aef519a..0b6699d 100644 --- a/src/test/java/hudson/plugin/versioncolumn/VersionMonitorTest.java +++ b/src/test/java/hudson/plugin/versioncolumn/VersionMonitorTest.java @@ -1,6 +1,10 @@ package hudson.plugin.versioncolumn; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.*; import hudson.Util; @@ -113,7 +117,7 @@ public void testMonitor_DifferentVersion_NotIgnored() throws IOException, Interr @Test public void testMonitor_DifferentVersion_Ignored() throws IOException, InterruptedException { VersionMonitor.DescriptorImpl descriptor = spy(new VersionMonitor.DescriptorImpl()); - doReturn(true).when(descriptor).isIgnored(); // Ensure isIgnored returns true + doReturn(true).when(descriptor).isIgnored(); // Ensure isIgnored returns true. Computer computer = mock(Computer.class); VirtualChannel channel = mock(VirtualChannel.class); @@ -131,7 +135,7 @@ public void testMonitor_DifferentVersion_Ignored() throws IOException, Interrupt } @Test - public void testMonitor_VersionIsNull() throws IOException, InterruptedException { + public void testMonitor_VersionIsNull_NotIgnored() throws IOException, InterruptedException { VersionMonitor.DescriptorImpl descriptor = spy(new VersionMonitor.DescriptorImpl()); doReturn(false).when(descriptor).isIgnored(); // Ensure isIgnored returns false @@ -150,6 +154,27 @@ public void testMonitor_VersionIsNull() throws IOException, InterruptedException verify(computer).setTemporarilyOffline(eq(true), any(VersionMonitor.RemotingVersionMismatchCause.class)); } + @Test + public void testMonitor_VersionIsNull_Ignored() throws IOException, InterruptedException { + VersionMonitor.DescriptorImpl descriptor = spy(new VersionMonitor.DescriptorImpl()); + doReturn(true).when(descriptor).isIgnored(); // Ensure isIgnored returns true. + + Computer computer = mock(Computer.class); + VirtualChannel channel = mock(VirtualChannel.class); + VersionMonitor.RemotingVersionMismatchCause cause = mock(VersionMonitor.RemotingVersionMismatchCause.class); + + when(computer.getChannel()).thenReturn(channel); + when(channel.call(ArgumentMatchers.>any())) + .thenReturn(null); + when(computer.isOffline()).thenReturn(true); + when(computer.getOfflineCause()).thenReturn(cause); + + String result = descriptor.monitor(computer); + + assertNull(result); + verify(computer).setTemporarilyOffline(eq(false), isNull()); + } + @Test public void testMonitor_OfflineDueToMismatch_VersionsMatch() throws IOException, InterruptedException { VersionMonitor.DescriptorImpl descriptor = new VersionMonitor.DescriptorImpl();