Skip to content

Commit 1d1b5f7

Browse files
Migrate tests to JUnit5 (#120)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor clean up Co-authored-by: strangelookingnerd <strangelookingnerd@users.noreply.github.com>
1 parent 2e8a02d commit 1d1b5f7

File tree

8 files changed

+79
-134
lines changed

8 files changed

+79
-134
lines changed

src/main/groovy/org/jenkinsci/plugins/sshsteps/util/Common.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Common {
3131
*/
3232
void validateRemote(remote) {
3333
assert remote, getPrefix() + "remote is null or empty"
34-
assert remote.name, getPrefix() + " a remote (or a gateway) is missing the required field 'name'"
34+
assert remote.name, getPrefix() + "a remote (or a gateway) is missing the required field 'name'"
3535
if (remote.retryCount)
3636
assert remote.retryCount >= 0, getPrefix() + "retryCount must be zero or positive ($remote.name)"
3737
if (remote.retryWaitSec)

src/test/java/org/jenkinsci/plugins/sshsteps/SSHServiceTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
import java.util.HashMap;
66
import java.util.Map;
7-
import org.junit.Test;
7+
import org.junit.jupiter.api.Test;
88

99
/**
1010
* Test cases for SSHService.
1111
*
1212
* @author Naresh Rayapati.
1313
*/
14-
public class SSHServiceTest {
14+
class SSHServiceTest {
1515

1616
@Test
17-
public void testWithEmptyRemoteThrowsAssertionError() {
17+
void testWithEmptyRemoteThrowsAssertionError() {
1818
Map<String, String> remote = new HashMap<>();
1919

2020
assertThatExceptionOfType(AssertionError.class)
@@ -25,20 +25,20 @@ public void testWithEmptyRemoteThrowsAssertionError() {
2525
}
2626

2727
@Test
28-
public void testRemoteWithEmptyNameThrowsAssertionError() {
28+
void testRemoteWithEmptyNameThrowsAssertionError() {
2929
Map<String, String> remote = new HashMap<>();
3030
remote.put("name", "");
3131

3232
assertThatExceptionOfType(AssertionError.class)
3333
.isThrownBy(() -> SSHService.create(remote, false, false, null))
3434
.withMessage(
35-
"SSH Steps: a remote (or a gateway) is missing the required field 'name'. Expression: remote.name")
35+
"SSH Steps: a remote (or a gateway) is missing the required field 'name'. Expression: remote.name")
3636
.withStackTraceContaining("AssertionError")
3737
.withNoCause();
3838
}
3939

4040
@Test
41-
public void testRemoteWithEmptyUserThrowsAssertionError() {
41+
void testRemoteWithEmptyUserThrowsAssertionError() {
4242
Map<String, String> remote = new HashMap<>();
4343
remote.put("name", "dummy");
4444

@@ -50,7 +50,7 @@ public void testRemoteWithEmptyUserThrowsAssertionError() {
5050
}
5151

5252
@Test
53-
public void testRemoteWithOutKnownHostsAndAllowAnyHostsThrowsIllegalArgumentException() {
53+
void testRemoteWithOutKnownHostsAndAllowAnyHostsThrowsIllegalArgumentException() {
5454
Map<String, String> remote = new HashMap<>();
5555
remote.put("name", "dummy");
5656
remote.put("user", "dummy");
@@ -63,7 +63,7 @@ public void testRemoteWithOutKnownHostsAndAllowAnyHostsThrowsIllegalArgumentExce
6363
}
6464

6565
@Test
66-
public void testRemoteWithMinimumRequiredParams() {
66+
void testRemoteWithMinimumRequiredParams() {
6767
Map<String, Object> remote = new HashMap<>();
6868
remote.put("name", "dummy");
6969
remote.put("user", "dummy");

src/test/java/org/jenkinsci/plugins/sshsteps/steps/BaseTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
package org.jenkinsci.plugins.sshsteps.steps;
22

3-
import static org.mockito.ArgumentMatchers.any;
4-
import static org.mockito.ArgumentMatchers.anyBoolean;
5-
import static org.mockito.Mockito.doNothing;
6-
import static org.mockito.Mockito.when;
7-
83
import hudson.EnvVars;
94
import hudson.Launcher;
105
import hudson.model.Run;
116
import hudson.model.TaskListener;
12-
import java.io.IOException;
13-
import java.io.PrintStream;
147
import org.jenkinsci.plugins.sshsteps.SSHService;
158
import org.jenkinsci.plugins.sshsteps.util.TestVirtualChannel;
169
import org.jenkinsci.plugins.workflow.steps.StepContext;
17-
import org.junit.After;
18-
import org.junit.Before;
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
1912
import org.mockito.Mock;
2013
import org.mockito.MockedStatic;
2114
import org.mockito.Mockito;
2215
import org.mockito.MockitoAnnotations;
2316

17+
import java.io.IOException;
18+
import java.io.PrintStream;
19+
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyBoolean;
22+
import static org.mockito.Mockito.doNothing;
23+
import static org.mockito.Mockito.when;
24+
2425
/**
2526
* Base Test Class.
2627
*
2728
* @author Naresh Rayapati
2829
*/
29-
public class BaseTest {
30+
class BaseTest {
3031

3132
@Mock
3233
TaskListener taskListenerMock;
@@ -46,8 +47,8 @@ public class BaseTest {
4647
private AutoCloseable closeable;
4748
private MockedStatic<SSHService> sshService;
4849

49-
@Before
50-
public void setUpBase() throws IOException, InterruptedException {
50+
@BeforeEach
51+
void setUpBase() throws IOException, InterruptedException {
5152

5253
closeable = MockitoAnnotations.openMocks(this);
5354

@@ -65,8 +66,8 @@ public void setUpBase() throws IOException, InterruptedException {
6566
when(contextMock.get(Launcher.class)).thenReturn(launcherMock);
6667
}
6768

68-
@After
69-
public void tearUpBase() throws Exception {
69+
@AfterEach
70+
void tearUpBase() throws Exception {
7071
sshService.close();
7172
closeable.close();
7273
}

src/test/java/org/jenkinsci/plugins/sshsteps/steps/CommandStepTest.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
11
package org.jenkinsci.plugins.sshsteps.steps;
22

3+
import org.junit.jupiter.api.Test;
4+
35
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4-
import static org.mockito.Mockito.any;
5-
import static org.mockito.Mockito.anyBoolean;
6-
import static org.mockito.Mockito.doNothing;
76
import static org.mockito.Mockito.times;
87
import static org.mockito.Mockito.verify;
9-
import static org.mockito.Mockito.when;
10-
11-
import hudson.EnvVars;
12-
import hudson.Launcher;
13-
import hudson.model.Run;
14-
import hudson.model.TaskListener;
15-
import java.io.IOException;
16-
import java.io.PrintStream;
17-
import org.jenkinsci.plugins.sshsteps.SSHService;
18-
import org.jenkinsci.plugins.sshsteps.util.TestVirtualChannel;
19-
import org.jenkinsci.plugins.workflow.steps.StepContext;
20-
import org.junit.Before;
21-
import org.junit.Test;
22-
import org.junit.runner.RunWith;
23-
import org.mockito.Mock;
248

259
/**
2610
* Unit test cases for CommandStep class.
2711
*
2812
* @author Naresh Rayapati
2913
*/
30-
public class CommandStepTest extends BaseTest {
14+
class CommandStepTest extends BaseTest {
3115

3216
CommandStep.Execution stepExecution;
3317

3418
@Test
35-
public void testWithEmptyCommandThrowsIllegalArgumentException() throws Exception {
19+
void testWithEmptyCommandThrowsIllegalArgumentException() throws Exception {
3620
final CommandStep step = new CommandStep("");
3721
stepExecution = new CommandStep.Execution(step, contextMock);
3822

@@ -45,7 +29,7 @@ public void testWithEmptyCommandThrowsIllegalArgumentException() throws Exceptio
4529
}
4630

4731
@Test
48-
public void testSuccessfulExecuteCommand() throws Exception {
32+
void testSuccessfulExecuteCommand() throws Exception {
4933
final CommandStep step = new CommandStep("ls -lrt");
5034

5135
// Since SSHService is a mock, it is not validating remote.

src/test/java/org/jenkinsci/plugins/sshsteps/steps/GetStepTest.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
package org.jenkinsci.plugins.sshsteps.steps;
22

3+
import hudson.FilePath;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.mockito.Mock;
7+
8+
import java.io.IOException;
9+
310
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
411
import static org.mockito.Mockito.any;
5-
import static org.mockito.Mockito.anyBoolean;
6-
import static org.mockito.Mockito.doNothing;
712
import static org.mockito.Mockito.times;
813
import static org.mockito.Mockito.verify;
914
import static org.mockito.Mockito.when;
1015

11-
import hudson.EnvVars;
12-
import hudson.FilePath;
13-
import hudson.Launcher;
14-
import hudson.model.Run;
15-
import hudson.model.TaskListener;
16-
import java.io.IOException;
17-
import java.io.PrintStream;
18-
import org.jenkinsci.plugins.sshsteps.SSHService;
19-
import org.jenkinsci.plugins.sshsteps.util.TestVirtualChannel;
20-
import org.jenkinsci.plugins.workflow.steps.StepContext;
21-
import org.junit.Before;
22-
import org.junit.Test;
23-
import org.junit.runner.RunWith;
24-
import org.mockito.Mock;
25-
2616
/**
2717
* Unit test cases for GetStep class.
2818
*
2919
* @author Naresh Rayapati
3020
*/
31-
public class GetStepTest extends BaseTest {
21+
class GetStepTest extends BaseTest {
3222

3323
final String path = "test.sh";
3424
final String filterBy = "name";
@@ -39,8 +29,8 @@ public class GetStepTest extends BaseTest {
3929

4030
GetStep.Execution stepExecution;
4131

42-
@Before
43-
public void setup() throws IOException, InterruptedException {
32+
@BeforeEach
33+
void setup() throws IOException, InterruptedException {
4434

4535
when(filePathMock.child(any())).thenReturn(filePathMock);
4636
when(filePathMock.exists()).thenReturn(true);
@@ -52,7 +42,7 @@ public void setup() throws IOException, InterruptedException {
5242
}
5343

5444
@Test
55-
public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
45+
void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
5646
final GetStep step = new GetStep("", path);
5747
stepExecution = new GetStep.Execution(step, contextMock);
5848

@@ -65,7 +55,7 @@ public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
6555
}
6656

6757
@Test
68-
public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
58+
void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
6959
final GetStep step = new GetStep(path, "");
7060
step.setOverride(true);
7161
stepExecution = new GetStep.Execution(step, contextMock);
@@ -79,7 +69,7 @@ public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
7969
}
8070

8171
@Test
82-
public void testSuccessfulExecuteScript() throws Exception {
72+
void testSuccessfulExecuteScript() throws Exception {
8373
final GetStep step = new GetStep(path, path);
8474
step.setOverride(true);
8575

src/test/java/org/jenkinsci/plugins/sshsteps/steps/PutStepTest.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
package org.jenkinsci.plugins.sshsteps.steps;
22

3+
import hudson.FilePath;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.mockito.Mock;
7+
8+
import java.io.IOException;
9+
310
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
411
import static org.mockito.Mockito.any;
5-
import static org.mockito.Mockito.anyBoolean;
6-
import static org.mockito.Mockito.doNothing;
712
import static org.mockito.Mockito.times;
813
import static org.mockito.Mockito.verify;
914
import static org.mockito.Mockito.when;
1015

11-
import hudson.EnvVars;
12-
import hudson.FilePath;
13-
import hudson.Launcher;
14-
import hudson.model.Run;
15-
import hudson.model.TaskListener;
16-
import java.io.IOException;
17-
import java.io.PrintStream;
18-
import org.jenkinsci.plugins.sshsteps.SSHService;
19-
import org.jenkinsci.plugins.sshsteps.util.TestVirtualChannel;
20-
import org.jenkinsci.plugins.workflow.steps.StepContext;
21-
import org.junit.Before;
22-
import org.junit.Test;
23-
import org.junit.runner.RunWith;
24-
import org.mockito.Mock;
25-
2616
/**
2717
* Unit test cases for PutStep class.
2818
*
2919
* @author Naresh Rayapati
3020
*/
31-
public class PutStepTest extends BaseTest {
21+
class PutStepTest extends BaseTest {
3222

3323
final String path = "test.sh";
3424
final String filterBy = "name";
@@ -39,8 +29,8 @@ public class PutStepTest extends BaseTest {
3929

4030
PutStep.Execution stepExecution;
4131

42-
@Before
43-
public void setup() throws IOException, InterruptedException {
32+
@BeforeEach
33+
void setup() throws IOException, InterruptedException {
4434

4535
when(filePathMock.child(any())).thenReturn(filePathMock);
4636
when(filePathMock.exists()).thenReturn(true);
@@ -52,7 +42,7 @@ public void setup() throws IOException, InterruptedException {
5242
}
5343

5444
@Test
55-
public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
45+
void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
5646
final PutStep step = new PutStep("", path);
5747
stepExecution = new PutStep.Execution(step, contextMock);
5848

@@ -65,7 +55,7 @@ public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
6555
}
6656

6757
@Test
68-
public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
58+
void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
6959
final PutStep step = new PutStep(path, "");
7060
stepExecution = new PutStep.Execution(step, contextMock);
7161

@@ -78,7 +68,7 @@ public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
7868
}
7969

8070
@Test
81-
public void testSuccessfulPut() throws Exception {
71+
void testSuccessfulPut() throws Exception {
8272
final PutStep step = new PutStep(path, path);
8373

8474
// Since SSHService is a mock, it is not validating remote.

0 commit comments

Comments
 (0)