Skip to content

Commit 932cb9d

Browse files
Migrate tests to JUnit5 (core / hudson / 1)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 22ae41d commit 932cb9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+928
-815
lines changed

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ THE SOFTWARE.
451451
</dependency>
452452
<dependency>
453453
<groupId>org.mockito</groupId>
454-
<artifactId>mockito-core</artifactId>
454+
<artifactId>mockito-junit-jupiter</artifactId>
455455
<scope>test</scope>
456456
</dependency>
457457
<dependency>

core/src/main/java/jenkins/security/ConfidentialStore.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jenkins.security;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import edu.umd.cs.findbugs.annotations.CheckForNull;
45
import edu.umd.cs.findbugs.annotations.NonNull;
56
import hudson.Extension;
@@ -19,6 +20,8 @@
1920
import java.util.logging.Logger;
2021
import jenkins.model.Jenkins;
2122
import org.kohsuke.MetaInfServices;
23+
import org.kohsuke.accmod.Restricted;
24+
import org.kohsuke.accmod.restrictions.NoExternalUse;
2225

2326
/**
2427
* The actual storage for the data held by {@link ConfidentialKey}s, and the holder
@@ -98,9 +101,11 @@ public abstract class ConfidentialStore {
98101
return cs;
99102
}
100103

101-
static final class Mock extends ConfidentialStore {
104+
@Restricted(NoExternalUse.class)
105+
@VisibleForTesting
106+
public static final class Mock extends ConfidentialStore {
102107

103-
static final Mock INSTANCE = new Mock();
108+
public static final Mock INSTANCE = new Mock();
104109

105110
private final SecureRandom rand;
106111

@@ -116,7 +121,7 @@ static final class Mock extends ConfidentialStore {
116121
rand.setSeed(new byte[] {1, 2, 3, 4});
117122
}
118123

119-
void clear() {
124+
public void clear() {
120125
data.clear();
121126
}
122127

core/src/test/java/hudson/cli/ListJobsCommandTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static org.hamcrest.Matchers.containsString;
55
import static org.hamcrest.Matchers.equalTo;
66
import static org.hamcrest.Matchers.is;
7-
import static org.junit.Assert.assertThrows;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
88
import static org.mockito.Mockito.mock;
99
import static org.mockito.Mockito.mockStatic;
1010
import static org.mockito.Mockito.when;
@@ -25,26 +25,26 @@
2525
import jenkins.model.Jenkins;
2626
import org.hamcrest.Description;
2727
import org.hamcrest.TypeSafeMatcher;
28-
import org.junit.Before;
29-
import org.junit.Test;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
3030
import org.mockito.MockedStatic;
3131
import org.mockito.Mockito;
3232

33-
public class ListJobsCommandTest {
33+
class ListJobsCommandTest {
3434

3535
private /*final*/ ListJobsCommand command;
3636
private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
3737
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
3838

39-
@Before
40-
public void setUp() {
39+
@BeforeEach
40+
void setUp() {
4141
command = mock(ListJobsCommand.class, Mockito.CALLS_REAL_METHODS);
4242
command.stdout = new PrintStream(stdout);
4343
command.stderr = new PrintStream(stderr);
4444
}
4545

4646
@Test
47-
public void failForNonexistentName() {
47+
void failForNonexistentName() {
4848
Jenkins jenkins = mock(Jenkins.class);
4949

5050
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
@@ -59,7 +59,7 @@ public void failForNonexistentName() {
5959
}
6060

6161
@Test
62-
public void getAllJobsForEmptyName() throws Exception {
62+
void getAllJobsForEmptyName() throws Exception {
6363

6464
final List<TopLevelItem> jenkinsJobs = Arrays.asList(
6565
job("some-job"), job("some-other-job")
@@ -76,7 +76,7 @@ public void getAllJobsForEmptyName() throws Exception {
7676
}
7777

7878
@Test
79-
public void getJobsFromView() throws Exception {
79+
void getJobsFromView() throws Exception {
8080

8181
final Collection<TopLevelItem> viewJobs = Arrays.asList(
8282
job("some-job"), job("some-other-job")
@@ -97,7 +97,7 @@ public void getJobsFromView() throws Exception {
9797
}
9898

9999
@Test
100-
public void getJobsRecursivelyFromViewGroup() throws Exception {
100+
void getJobsRecursivelyFromViewGroup() throws Exception {
101101

102102
final CompositeView rootView = mock(CompositeView.class);
103103
when(rootView.getAllItems()).thenCallRealMethod();

core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
package hudson.cli.handlers;
2626

27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.assertNull;
29-
import static org.junit.Assert.assertThrows;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNull;
29+
import static org.junit.jupiter.api.Assertions.assertThrows;
3030
import static org.mockito.Mockito.doThrow;
3131
import static org.mockito.Mockito.mock;
3232
import static org.mockito.Mockito.mockStatic;
@@ -40,9 +40,9 @@
4040
import hudson.security.ACL;
4141
import hudson.security.Permission;
4242
import jenkins.model.Jenkins;
43-
import org.junit.After;
44-
import org.junit.Before;
45-
import org.junit.Test;
43+
import org.junit.jupiter.api.AfterEach;
44+
import org.junit.jupiter.api.BeforeEach;
45+
import org.junit.jupiter.api.Test;
4646
import org.kohsuke.args4j.CmdLineException;
4747
import org.kohsuke.args4j.spi.Parameters;
4848
import org.kohsuke.args4j.spi.Setter;
@@ -52,7 +52,7 @@
5252
import org.springframework.security.access.AccessDeniedException;
5353
import org.springframework.security.core.Authentication;
5454

55-
public class ViewOptionHandlerTest {
55+
class ViewOptionHandlerTest {
5656

5757
@Mock private Setter<View> setter;
5858
private ViewOptionHandler handler;
@@ -65,12 +65,13 @@ public class ViewOptionHandlerTest {
6565

6666
private AutoCloseable mocks;
6767

68-
@After
69-
public void tearDown() throws Exception {
68+
@AfterEach
69+
void tearDown() throws Exception {
7070
mocks.close();
7171
}
7272

73-
@Before public void setUp() {
73+
@BeforeEach
74+
void setUp() {
7475

7576
mocks = MockitoAnnotations.openMocks(this);
7677

@@ -88,7 +89,8 @@ public void tearDown() throws Exception {
8889
when(outer.getView("nested")).thenReturn(nested);
8990
}
9091

91-
@Test public void resolveTopLevelView() throws Exception {
92+
@Test
93+
void resolveTopLevelView() throws Exception {
9294
Jenkins jenkins = mock(Jenkins.class);
9395
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
9496
mockJenkins(mocked, jenkins);
@@ -110,7 +112,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
110112
});
111113
}
112114

113-
@Test public void resolveNestedView() throws Exception {
115+
@Test
116+
void resolveNestedView() throws Exception {
114117
Jenkins jenkins = mock(Jenkins.class);
115118
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
116119
mockJenkins(mocked, jenkins);
@@ -120,7 +123,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
120123
}
121124
}
122125

123-
@Test public void resolveOuterView() throws Exception {
126+
@Test
127+
void resolveOuterView() throws Exception {
124128
Jenkins jenkins = mock(Jenkins.class);
125129
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
126130
mockJenkins(mocked, jenkins);
@@ -130,7 +134,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
130134
}
131135
}
132136

133-
@Test public void ignoreLeadingAndTrailingSlashes() throws Exception {
137+
@Test
138+
void ignoreLeadingAndTrailingSlashes() throws Exception {
134139
Jenkins jenkins = mock(Jenkins.class);
135140
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
136141
mockJenkins(mocked, jenkins);
@@ -140,7 +145,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
140145
}
141146
}
142147

143-
@Test public void reportNonexistentTopLevelView() throws Exception {
148+
@Test
149+
void reportNonexistentTopLevelView() {
144150
Jenkins jenkins = mock(Jenkins.class);
145151
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
146152
mockJenkins(mocked, jenkins);
@@ -154,7 +160,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
154160
}
155161
}
156162

157-
@Test public void reportNonexistentNestedView() throws Exception {
163+
@Test
164+
void reportNonexistentNestedView() {
158165
Jenkins jenkins = mock(Jenkins.class);
159166
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
160167
mockJenkins(mocked, jenkins);
@@ -168,7 +175,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
168175
}
169176
}
170177

171-
@Test public void reportNonexistentInnerView() throws Exception {
178+
@Test
179+
void reportNonexistentInnerView() {
172180
Jenkins jenkins = mock(Jenkins.class);
173181
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
174182
mockJenkins(mocked, jenkins);
@@ -182,7 +190,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
182190
}
183191
}
184192

185-
@Test public void reportTraversingViewThatIsNotAViewGroup() throws Exception {
193+
@Test
194+
void reportTraversingViewThatIsNotAViewGroup() {
186195
Jenkins jenkins = mock(Jenkins.class);
187196
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
188197
mockJenkins(mocked, jenkins);
@@ -196,7 +205,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
196205
}
197206
}
198207

199-
@Test public void reportEmptyViewNameRequestAsNull() {
208+
@Test
209+
void reportEmptyViewNameRequestAsNull() {
200210
Jenkins jenkins = mock(Jenkins.class);
201211
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
202212
mockJenkins(mocked, jenkins);
@@ -205,18 +215,19 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
205215
}
206216
}
207217

208-
@Test public void reportViewSpaceNameRequestAsIAE() {
218+
@Test
219+
void reportViewSpaceNameRequestAsIAE() {
209220
Jenkins jenkins = mock(Jenkins.class);
210221
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
211222
mockJenkins(mocked, jenkins);
212-
final IllegalArgumentException e = assertThrows("No exception thrown. Expected IllegalArgumentException",
213-
IllegalArgumentException.class, () -> assertNull(handler.getView(" ")));
223+
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> assertNull(handler.getView(" ")), "No exception thrown. Expected IllegalArgumentException");
214224
assertEquals("No view named inside view Jenkins", e.getMessage());
215225
verifyNoInteractions(setter);
216226
}
217227
}
218228

219-
@Test public void reportNullViewAsNPE() {
229+
@Test
230+
void reportNullViewAsNPE() {
220231
Jenkins jenkins = mock(Jenkins.class);
221232
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
222233
mockJenkins(mocked, jenkins);
@@ -225,7 +236,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
225236
}
226237
}
227238

228-
@Test public void refuseToReadOuterView() throws Exception {
239+
@Test
240+
void refuseToReadOuterView() {
229241
Jenkins jenkins = mock(Jenkins.class);
230242
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
231243
mockJenkins(mocked, jenkins);
@@ -245,7 +257,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
245257
}
246258
}
247259

248-
@Test public void refuseToReadNestedView() throws Exception {
260+
@Test
261+
void refuseToReadNestedView() {
249262
Jenkins jenkins = mock(Jenkins.class);
250263
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
251264
mockJenkins(mocked, jenkins);
@@ -264,7 +277,8 @@ public boolean hasPermission2(@NonNull Authentication a, @NonNull Permission p)
264277
}
265278
}
266279

267-
@Test public void refuseToReadInnerView() throws Exception {
280+
@Test
281+
void refuseToReadInnerView() {
268282
Jenkins jenkins = mock(Jenkins.class);
269283
try (MockedStatic<Jenkins> mocked = mockStatic(Jenkins.class)) {
270284
mockJenkins(mocked, jenkins);

core/src/test/java/hudson/console/LineTransformationOutputStreamTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@
3131
import java.io.IOException;
3232
import java.nio.charset.StandardCharsets;
3333
import java.util.concurrent.atomic.AtomicLong;
34-
import org.junit.Test;
34+
import org.junit.jupiter.api.Test;
3535

36-
public final class LineTransformationOutputStreamTest {
36+
final class LineTransformationOutputStreamTest {
3737

38-
@Test public void nl() throws Exception {
38+
@Test
39+
void nl() throws Exception {
3940
test("\n");
4041
}
4142

42-
@Test public void crnl() throws Exception {
43+
@Test
44+
void crnl() throws Exception {
4345
test("\r\n");
4446
}
4547

46-
@Test public void cr() throws Exception {
48+
@Test
49+
void cr() throws Exception {
4750
test("\r");
4851
}
4952

core/src/test/java/hudson/console/UrlAnnotatorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
/**
3434
* @author Alan Harder
3535
*/
36-
public class UrlAnnotatorTest {
36+
class UrlAnnotatorTest {
3737

3838
private ConsoleAnnotator<?> ca = new UrlAnnotator().newInstance(null);
3939

4040
@Test
41-
public void testAnnotate() {
41+
void testAnnotate() {
4242
assertEquals("Hello &lt;foo&gt;<a href='http://foo/'>http://foo/</a>&lt;/foo&gt; Bye",
4343
annotate("Hello <foo>http://foo/</foo> Bye"));
4444

@@ -60,7 +60,7 @@ public void testAnnotate() {
6060

6161
@Test
6262
@Issue("JENKINS-19866")
63-
public void annotateFileScheme() {
63+
void annotateFileScheme() {
6464
assertEquals(
6565
"Get this <a href='file://here/in/this/folder/'>file://here/in/this/folder/</a>.",
6666
annotate("Get this file://here/in/this/folder/.")

0 commit comments

Comments
 (0)