Skip to content

Commit 76a0bd4

Browse files
Migrate tests to JUnit5 (#109)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent e731673 commit 76a0bd4

12 files changed

+182
-195
lines changed

src/test/java/org/jenkinsci/plugin/gitea/GiteaBrowserTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,58 +27,58 @@
2727
import hudson.plugins.git.GitChangeSet;
2828
import hudson.plugins.git.GitChangeSet.Path;
2929
import java.io.InputStream;
30-
import org.junit.Test;
30+
import org.junit.jupiter.api.Test;
3131

32+
import static org.hamcrest.MatcherAssert.assertThat;
3233
import static org.hamcrest.Matchers.is;
3334
import static org.hamcrest.Matchers.nullValue;
34-
import static org.junit.Assert.assertThat;
3535

36-
public class GiteaBrowserTest {
36+
class GiteaBrowserTest {
3737

3838
private static final String GITEA_URL = "http://gitea.test";
3939

4040
private final GiteaBrowser instance = new GiteaBrowser(GITEA_URL);
4141

4242
@Test
43-
public void given__instance__when__getUrl__then__urlEndsWithSlash() throws Exception {
43+
void given__instance__when__getUrl__then__urlEndsWithSlash() throws Exception {
4444
assertThat(instance.getUrl().toString(),
4545
is(GITEA_URL + "/"));
4646
}
4747

4848
@Test
49-
public void given__instance__when__nonNormalizedUrl__then__urlNormalized() throws Exception {
49+
void given__instance__when__nonNormalizedUrl__then__urlNormalized() throws Exception {
5050
assertThat(new GiteaBrowser("http://gitea.test:80/foo/../bar/..").getUrl().toString(),
5151
is(GITEA_URL + "/"));
5252
}
5353

5454
@Test
55-
public void given__instance__when__repoUrl_ends_with_slash__then__no_double_slash() throws Exception {
55+
void given__instance__when__repoUrl_ends_with_slash__then__no_double_slash() throws Exception {
5656
assertThat(new GiteaBrowser(GITEA_URL + "/").getUrl().toString(),
5757
is(GITEA_URL + "/"));
5858
}
5959

6060
@Test
61-
public void given__changeset__when__getChangeSetLink__then__url_generater() throws Exception {
61+
void given__changeset__when__getChangeSetLink__then__url_generater() throws Exception {
6262
assertThat(instance.getChangeSetLink(loadChangeSet("rawchangelog")).toString(),
6363
is(GITEA_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d"));
6464
}
6565

6666
@Test
67-
public void given__changeset__when__getDiffLink_first_file__then__fragment_1() throws Exception {
67+
void given__changeset__when__getDiffLink_first_file__then__fragment_1() throws Exception {
6868
assertThat(instance.getDiffLink(
6969
findPath(loadChangeSet("rawchangelog"), "src/main/java/hudson/plugins/git/browser/GithubWeb.java")
7070
).toString(), is(GITEA_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d#diff-1"));
7171
}
7272

7373
@Test
74-
public void given__changeset__when__getDiffLink_second_file__then__fragment_2() throws Exception {
74+
void given__changeset__when__getDiffLink_second_file__then__fragment_2() throws Exception {
7575
assertThat(instance.getDiffLink(
7676
findPath(loadChangeSet("rawchangelog"), "src/test/java/hudson/plugins/git/browser/GithubWebTest.java")
7777
).toString(), is(GITEA_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d#diff-2"));
7878
}
7979

8080
@Test
81-
public void given__changeset__when__getDiffLink_new_file__then__no_diff_link() throws Exception {
81+
void given__changeset__when__getDiffLink_new_file__then__no_diff_link() throws Exception {
8282
assertThat(instance.getDiffLink(
8383
findPath(
8484
loadChangeSet("rawchangelog"),
@@ -88,7 +88,7 @@ public void given__changeset__when__getDiffLink_new_file__then__no_diff_link() t
8888
}
8989

9090
@Test
91-
public void given__path__when__getFileLink_existing_file__then__file_link_points_to_file() throws Exception {
91+
void given__path__when__getFileLink_existing_file__then__file_link_points_to_file() throws Exception {
9292
assertThat(instance.getFileLink(
9393
findPath(
9494
loadChangeSet("rawchangelog"),
@@ -101,19 +101,19 @@ public void given__path__when__getFileLink_existing_file__then__file_link_points
101101
}
102102

103103
@Test
104-
public void given__path__when__getFileLink_deleted_file__then__file_link_points_to_diff() throws Exception {
104+
void given__path__when__getFileLink_deleted_file__then__file_link_points_to_diff() throws Exception {
105105
assertThat(instance.getFileLink(
106106
findPath(loadChangeSet("rawchangelog-with-deleted-file"), "bar")
107107
).toString(), is(GITEA_URL + "/commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#diff-1"));
108108
}
109109

110110
private GitChangeSet loadChangeSet(String resourceName) throws Exception {
111111
try (InputStream is = GiteaBrowserTest.class.getResourceAsStream(resourceName)) {
112-
return new GitChangeLogParser(false).parse(is).get(0);
112+
return new GitChangeLogParser(null, false).parse(is).get(0);
113113
}
114114
}
115115

116-
private Path findPath(GitChangeSet changeSet, String path) throws Exception {
116+
private Path findPath(GitChangeSet changeSet, String path) {
117117
for (final Path p : changeSet.getPaths()) {
118118
if (path.equals(p.getPath())) {
119119
return p;

src/test/java/org/jenkinsci/plugin/gitea/GiteaCreateSCMEventTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,51 @@
99
import org.jenkinsci.plugin.gitea.client.api.GiteaCreateEvent;
1010
import org.jenkinsci.plugin.gitea.client.api.GiteaOwner;
1111
import org.jenkinsci.plugin.gitea.client.api.GiteaRepository;
12-
import org.junit.Test;
12+
import org.junit.jupiter.api.Test;
1313

14-
import static org.junit.Assert.assertThat;
14+
import static org.hamcrest.MatcherAssert.assertThat;
1515
import static org.hamcrest.Matchers.is;
1616

17-
public class GiteaCreateSCMEventTest {
17+
class GiteaCreateSCMEventTest {
1818

19-
GiteaSCMSource giteaSCMSource = new GiteaSCMSource("", "", "");
20-
MockSCMSource scmSource = new MockSCMSource("controllerId", "giteaRepo", new ArrayList<>());
21-
MockSCMNavigator scmNavigator = new MockSCMNavigator("controllerId", new ArrayList<>());
22-
GiteaCreateSCMEvent branchCreateEvent = new GiteaCreateSCMEvent(withGiteaCreateEvent("feature/branch", "branch"), "?");
23-
GiteaCreateSCMEvent tagCreateEvent = new GiteaCreateSCMEvent(withGiteaCreateEvent("mytag", "tag"), "?");
19+
private final GiteaSCMSource giteaSCMSource = new GiteaSCMSource("", "", "");
20+
private final MockSCMSource scmSource = new MockSCMSource("controllerId", "giteaRepo", new ArrayList<>());
21+
private final MockSCMNavigator scmNavigator = new MockSCMNavigator("controllerId", new ArrayList<>());
22+
private final GiteaCreateSCMEvent branchCreateEvent = new GiteaCreateSCMEvent(withGiteaCreateEvent("feature/branch", "branch"), "?");
23+
private final GiteaCreateSCMEvent tagCreateEvent = new GiteaCreateSCMEvent(withGiteaCreateEvent("mytag", "tag"), "?");
2424

2525
@Test
26-
public void descriptionForSCMNavigator_withBranchCreateEvent() {
26+
void descriptionForSCMNavigator_withBranchCreateEvent() {
2727
assertThat(branchCreateEvent.descriptionFor(scmNavigator), is("Create event for branch feature/branch in repository giteaRepo"));
2828
}
2929

3030
@Test
31-
public void descriptionForSCMNavigator_withTagCreateEvent() {
31+
void descriptionForSCMNavigator_withTagCreateEvent() {
3232
assertThat(tagCreateEvent.descriptionFor(scmNavigator), is("Create event for tag mytag in repository giteaRepo"));
3333
}
3434

3535
@Test
36-
public void descriptionForSCMSource_withBranchCreateEvent() {
36+
void descriptionForSCMSource_withBranchCreateEvent() {
3737
assertThat(branchCreateEvent.descriptionFor(scmSource), is("Create event for branch feature/branch"));
3838
}
3939

4040
@Test
41-
public void descriptionForSCMSource_withTagCreateEvent() {
41+
void descriptionForSCMSource_withTagCreateEvent() {
4242
assertThat(tagCreateEvent.descriptionFor(scmSource), is("Create event for tag mytag"));
4343
}
4444

4545
@Test
46-
public void description_withBranchCreateEvent() {
46+
void description_withBranchCreateEvent() {
4747
assertThat(branchCreateEvent.description(), is("Create event for branch feature/branch in repository ownerUser/giteaRepo"));
4848
}
4949

5050
@Test
51-
public void description_withTagCreateEvent() {
51+
void description_withTagCreateEvent() {
5252
assertThat(tagCreateEvent.description(), is("Create event for tag mytag in repository ownerUser/giteaRepo"));
5353
}
5454

5555
@Test
56-
public void headsFor_withBranchCreateEvent() {
56+
void headsFor_withBranchCreateEvent() {
5757
Map<SCMHead, SCMRevision> headsFor = branchCreateEvent.headsFor(giteaSCMSource);
5858

5959
assertThat(headsFor.size(), is(1));
@@ -65,7 +65,7 @@ public void headsFor_withBranchCreateEvent() {
6565
}
6666

6767
@Test
68-
public void headsFor_withTagCreateEvent() {
68+
void headsFor_withTagCreateEvent() {
6969
Map<SCMHead, SCMRevision> headsFor = tagCreateEvent.headsFor(giteaSCMSource);
7070

7171
assertThat(headsFor.size(), is(1));

src/test/java/org/jenkinsci/plugin/gitea/GiteaDeleteSCMEventTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,52 @@
99
import org.jenkinsci.plugin.gitea.client.api.GiteaDeleteEvent;
1010
import org.jenkinsci.plugin.gitea.client.api.GiteaOwner;
1111
import org.jenkinsci.plugin.gitea.client.api.GiteaRepository;
12-
import org.junit.Test;
12+
import org.junit.jupiter.api.Test;
1313

14+
import static org.hamcrest.MatcherAssert.assertThat;
1415
import static org.hamcrest.Matchers.is;
1516
import static org.hamcrest.Matchers.nullValue;
16-
import static org.junit.Assert.assertThat;
1717

18-
public class GiteaDeleteSCMEventTest {
18+
class GiteaDeleteSCMEventTest {
1919

20-
GiteaSCMSource giteaSCMSource = new GiteaSCMSource("", "", "");
21-
MockSCMSource scmSource = new MockSCMSource("controllerId", "giteaRepo", new ArrayList<>());
22-
MockSCMNavigator scmNavigator = new MockSCMNavigator("controllerId", new ArrayList<>());
23-
GiteaDeleteSCMEvent branchDeleteEvent = new GiteaDeleteSCMEvent(withGiteaDeleteEvent("feature/branch", "branch"), "?");
24-
GiteaDeleteSCMEvent tagDeleteEvent = new GiteaDeleteSCMEvent(withGiteaDeleteEvent("mytag", "tag"), "?");
20+
final GiteaSCMSource giteaSCMSource = new GiteaSCMSource("", "", "");
21+
final MockSCMSource scmSource = new MockSCMSource("controllerId", "giteaRepo", new ArrayList<>());
22+
final MockSCMNavigator scmNavigator = new MockSCMNavigator("controllerId", new ArrayList<>());
23+
final GiteaDeleteSCMEvent branchDeleteEvent = new GiteaDeleteSCMEvent(withGiteaDeleteEvent("feature/branch", "branch"), "?");
24+
final GiteaDeleteSCMEvent tagDeleteEvent = new GiteaDeleteSCMEvent(withGiteaDeleteEvent("mytag", "tag"), "?");
2525

2626
@Test
27-
public void descriptionForSCMNavigator_withBranchDeleteEvent() {
27+
void descriptionForSCMNavigator_withBranchDeleteEvent() {
2828
assertThat(branchDeleteEvent.descriptionFor(scmNavigator), is("Delete event for branch feature/branch in repository giteaRepo"));
2929
}
3030

3131
@Test
32-
public void descriptionForSCMNavigator_withTagDeleteEvent() {
32+
void descriptionForSCMNavigator_withTagDeleteEvent() {
3333
assertThat(tagDeleteEvent.descriptionFor(scmNavigator), is("Delete event for tag mytag in repository giteaRepo"));
3434
}
3535

3636
@Test
37-
public void descriptionForSCMSource_withBranchDeleteEvent() {
37+
void descriptionForSCMSource_withBranchDeleteEvent() {
3838
assertThat(branchDeleteEvent.descriptionFor(scmSource), is("Delete event for branch feature/branch"));
3939
}
4040

4141
@Test
42-
public void descriptionForSCMSource_withTagDeleteEvent() {
42+
void descriptionForSCMSource_withTagDeleteEvent() {
4343
assertThat(tagDeleteEvent.descriptionFor(scmSource), is("Delete event for tag mytag"));
4444
}
4545

4646
@Test
47-
public void description_withBranchDeleteEvent() {
47+
void description_withBranchDeleteEvent() {
4848
assertThat(branchDeleteEvent.description(), is("Delete event for branch feature/branch in repository ownerUser/giteaRepo"));
4949
}
5050

5151
@Test
52-
public void description_withTagDeleteEvent() {
52+
void description_withTagDeleteEvent() {
5353
assertThat(tagDeleteEvent.description(), is("Delete event for tag mytag in repository ownerUser/giteaRepo"));
5454
}
5555

5656
@Test
57-
public void headsFor_withBranchDeleteEvent() {
57+
void headsFor_withBranchDeleteEvent() {
5858
Map<SCMHead, SCMRevision> headsFor = branchDeleteEvent.headsFor(giteaSCMSource);
5959

6060
assertThat(headsFor.size(), is(1));
@@ -66,7 +66,7 @@ public void headsFor_withBranchDeleteEvent() {
6666
}
6767

6868
@Test
69-
public void headsFor_withTagDeleteEvent() {
69+
void headsFor_withTagDeleteEvent() {
7070
Map<SCMHead, SCMRevision> headsFor = tagDeleteEvent.headsFor(giteaSCMSource);
7171

7272
assertThat(headsFor.size(), is(1));

src/test/java/org/jenkinsci/plugin/gitea/authentication/GiteaAuthSourceTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,20 @@
3030
import org.jenkinsci.plugin.gitea.client.api.GiteaAuthToken;
3131
import org.jenkinsci.plugin.gitea.client.api.GiteaAuthUser;
3232
import org.jenkinsci.plugin.gitea.credentials.PersonalAccessToken;
33-
import org.junit.ClassRule;
34-
import org.junit.Test;
33+
import org.junit.jupiter.api.Test;
3534
import org.jvnet.hudson.test.JenkinsRule;
35+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3636
import org.mockito.Mockito;
3737

38+
import static org.hamcrest.MatcherAssert.assertThat;
3839
import static org.hamcrest.Matchers.instanceOf;
3940
import static org.hamcrest.Matchers.is;
40-
import static org.junit.Assert.assertThat;
4141

42-
public class GiteaAuthSourceTest {
43-
@ClassRule
44-
public static JenkinsRule j = new JenkinsRule();
42+
@WithJenkins
43+
class GiteaAuthSourceTest {
4544

4645
@Test
47-
public void given__tokenCredential__when__convert__then__tokenAuth() throws Exception {
46+
void given__tokenCredential__when__convert__then__tokenAuth(JenkinsRule j) {
4847
// we use a mock to ensure that java.lang.reflect.Proxy implementations of the credential interface work
4948
PersonalAccessToken credential = Mockito.mock(PersonalAccessToken.class);
5049
Mockito.when(credential.getToken()).thenReturn(Secret.fromString("b5bc10f13665362bd61de931c731e3c74187acc4"));
@@ -54,7 +53,7 @@ public void given__tokenCredential__when__convert__then__tokenAuth() throws Exce
5453
}
5554

5655
@Test
57-
public void given__userPassCredential__when__convert__then__tokenAuth() throws Exception {
56+
void given__userPassCredential__when__convert__then__tokenAuth(JenkinsRule j) {
5857
// we use a mock to ensure that java.lang.reflect.Proxy implementations of the credential interface work
5958
UsernamePasswordCredentials credential = Mockito.mock(UsernamePasswordCredentials.class);
6059
Mockito.when(credential.getUsername()).thenReturn("bob");

src/test/java/org/jenkinsci/plugin/gitea/client/api/GiteaConnectionBuilderJenkinsTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22

33
import org.jenkinsci.plugin.gitea.client.mock.MockGiteaConnection;
44
import org.jenkinsci.plugin.gitea.client.mock.MockGiteaConnectionFactory;
5-
import org.junit.ClassRule;
6-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
76
import org.jvnet.hudson.test.JenkinsRule;
7+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
88

9+
import static org.hamcrest.MatcherAssert.assertThat;
910
import static org.hamcrest.Matchers.instanceOf;
1011
import static org.hamcrest.Matchers.not;
11-
import static org.junit.Assert.assertThat;
1212

13-
public class GiteaConnectionBuilderJenkinsTest {
14-
15-
@ClassRule
16-
public static JenkinsRule j = new JenkinsRule();
13+
@WithJenkins
14+
class GiteaConnectionBuilderJenkinsTest {
1715

1816
@Test
19-
public void given__registered_mock__when__open__then__mock_returned() throws Exception {
17+
void given__registered_mock__when__open__then__mock_returned(JenkinsRule j) throws Exception {
2018
MockGiteaConnectionFactory.reset();
2119
MockGiteaConnectionFactory.register(new MockGiteaConnection("bob"), "http://gitea.test/open");
2220
assertThat(Gitea.server("http://gitea.test/open").open(), instanceOf(MockGiteaConnection.class));
2321
}
2422

2523
@Test
26-
public void given__no_registered_mock__when__open__then__real_returned() throws Exception {
24+
void given__no_registered_mock__when__open__then__real_returned(JenkinsRule j) throws Exception {
2725
MockGiteaConnectionFactory.reset();
2826
assertThat(Gitea.server("http://gitea.test/open").open(), not(instanceOf(MockGiteaConnection.class)));
2927
}

src/test/java/org/jenkinsci/plugin/gitea/client/api/GiteaConnectionBuilderTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22

33
import org.jenkinsci.plugin.gitea.client.mock.MockGiteaConnection;
44
import org.jenkinsci.plugin.gitea.client.mock.MockGiteaConnectionFactory;
5-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
66

7+
import static org.hamcrest.MatcherAssert.assertThat;
78
import static org.hamcrest.Matchers.instanceOf;
89
import static org.hamcrest.Matchers.not;
9-
import static org.junit.Assert.assertThat;
1010

11-
public class GiteaConnectionBuilderTest {
11+
class GiteaConnectionBuilderTest {
12+
1213
@Test
13-
public void given__registered_mock__when__open__then__mock_returned() throws Exception {
14+
void given__registered_mock__when__open__then__mock_returned() throws Exception {
1415
MockGiteaConnectionFactory.reset();
1516
MockGiteaConnectionFactory.register(new MockGiteaConnection("bob"), "http://gitea.test/open");
1617
assertThat(Gitea.server("http://gitea.test/open").open(), instanceOf(MockGiteaConnection.class));
1718
}
1819

1920
@Test
20-
public void given__no_registered_mock__when__open__then__real_returned() throws Exception {
21+
void given__no_registered_mock__when__open__then__real_returned() throws Exception {
2122
MockGiteaConnectionFactory.reset();
2223
assertThat(Gitea.server("http://gitea.test/open").open(), not(instanceOf(MockGiteaConnection.class)));
2324
}

0 commit comments

Comments
 (0)