Skip to content

Commit 3e8590e

Browse files
mureinikmbax
authored andcommitted
Migrate tests to modern JUnit Jupiter
The project currently uses the outdated JUnit 4.13.2. In order to make it easier to write tests and easier for future contributors to easily start working with the project, this patch migrates the test suite to the modern JUnit Jupiter. This patch contains the following changes: - Dependencies: - The junit:junit:4.13.2 dependency was replaced with org.junit.jupiter:junit-jupiter:5.8.1 - Unlike JUnit 4, JUnit Jupiter does not bundle the hamcrest library, so the explicit dependency org.hamcrest:hamcrest:2.2 was added - Annotations - org.junit.jupiter.api.BeforeEach was used as a drop-in replacement for org.junit.Before - org.junit.jupiter.api.Test was used as a drop-in replacement for org.junit.Test without arguments. See "Assertions" below for handling of org.junit.Test with an "expected" argument - Assertions - org.junit.jupiter.api.Assertions was used as a drop-in replacement for org.junit.Assert in the cases the "message" argument was not used. In the cases a "message" argument was used it was moved to be the last argument instead of the first argument - org.hamcrest.MatcherAssert#assertThat was used as a drop-in replacement for org.junit.Assert#assertThat - org.junit.jupiter.api.Assertions#assertThrows was used to assert cases where a method call should throw an exception instead of passing an "expected" argument to the @test annotation. As a side bonus, this also makes the tests stricter, as it asserts the actual method call threw the expected exception and not the code used to set up its arguments/environment. - Misc - Tests using org.junit.rules.TemporaryFolder were rewritten to use the similar org.junit.jupiter.api.io.TempDir. While these two classes conceptually have the same functionality their APIs are different, and this is not a drop-in replacement.
1 parent 262113a commit 3e8590e

39 files changed

+486
-475
lines changed

pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@
8989
<scope>provided</scope>
9090
</dependency>
9191
<dependency>
92-
<groupId>junit</groupId>
93-
<artifactId>junit</artifactId>
94-
<version>4.13.2</version>
92+
<groupId>org.junit.jupiter</groupId>
93+
<artifactId>junit-jupiter-engine</artifactId>
94+
<version>5.8.1</version>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.hamcrest</groupId>
99+
<artifactId>hamcrest</artifactId>
100+
<version>2.2</version>
95101
<scope>test</scope>
96102
</dependency>
97103
<dependency>

src/test/java/org/kitteh/irc/client/library/CapabilityManagerTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.kitteh.irc.client.library;
22

33
import org.checkerframework.checker.nullness.qual.NonNull;
4-
import org.junit.Assert;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
66
import org.kitteh.irc.client.library.element.CapabilityState;
77
import org.kitteh.irc.client.library.feature.CapabilityManager;
88

@@ -23,11 +23,11 @@ public class CapabilityManagerTest {
2323
@Test
2424
public void testDefaultGetCapabilityMethodsInCapabilityManager() {
2525
StubCapabilityManager sut = new StubCapabilityManager();
26-
Assert.assertEquals(2, sut.getCapabilities().size());
27-
Assert.assertTrue(sut.getCapability("Test1").isPresent());
28-
Assert.assertTrue(sut.getSupportedCapability("Test1").isPresent());
29-
Assert.assertFalse(sut.getSupportedCapability("Test2").isPresent());
30-
Assert.assertFalse(sut.getCapability("Cats").isPresent());
26+
Assertions.assertEquals(2, sut.getCapabilities().size());
27+
Assertions.assertTrue(sut.getCapability("Test1").isPresent());
28+
Assertions.assertTrue(sut.getSupportedCapability("Test1").isPresent());
29+
Assertions.assertFalse(sut.getSupportedCapability("Test2").isPresent());
30+
Assertions.assertFalse(sut.getCapability("Cats").isPresent());
3131
}
3232

3333
/**
@@ -36,8 +36,8 @@ public void testDefaultGetCapabilityMethodsInCapabilityManager() {
3636
@Test
3737
public void testNativeCapabilityRetrieval() {
3838
List<String> caps = CapabilityManager.Defaults.getDefaults();
39-
Assert.assertFalse(caps.isEmpty());
40-
Assert.assertTrue(caps.contains(CapabilityManager.Defaults.ACCOUNT_NOTIFY));
39+
Assertions.assertFalse(caps.isEmpty());
40+
Assertions.assertTrue(caps.contains(CapabilityManager.Defaults.ACCOUNT_NOTIFY));
4141
}
4242

4343
/**
@@ -48,7 +48,7 @@ public void testNativeCapabilityRetrieval() {
4848
@Test
4949
public void testConstructorIsPrivate() throws Exception {
5050
Constructor<CapabilityManager.Defaults> constructor = CapabilityManager.Defaults.class.getDeclaredConstructor();
51-
Assert.assertTrue(Modifier.isPrivate(constructor.getModifiers()));
51+
Assertions.assertTrue(Modifier.isPrivate(constructor.getModifiers()));
5252
constructor.setAccessible(true);
5353
constructor.newInstance();
5454
}

src/test/java/org/kitteh/irc/client/library/CaseMappingTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.kitteh.irc.client.library;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
55
import org.kitteh.irc.client.library.feature.CaseMapping;
66
import org.kitteh.irc.client.library.util.Pair;
77

@@ -20,10 +20,10 @@ public class CaseMappingTest {
2020
public void verifyMatch() {
2121
for (CaseMapping caseMapping : CaseMapping.values()) {
2222
Optional<CaseMapping> acquired = CaseMapping.getByName(caseMapping.name().replace('_', '-'));
23-
Assert.assertTrue("Failed to acquire mapping for " + caseMapping.name(), acquired.isPresent());
24-
Assert.assertEquals(caseMapping, acquired.get());
23+
Assertions.assertTrue(acquired.isPresent(), "Failed to acquire mapping for " + caseMapping.name());
24+
Assertions.assertEquals(caseMapping, acquired.get());
2525
}
26-
Assert.assertEquals(Optional.empty(), CaseMapping.getByName(null));
26+
Assertions.assertEquals(Optional.empty(), CaseMapping.getByName(null));
2727
}
2828

2929
/**
@@ -37,11 +37,11 @@ public void lowerCase() {
3737
test.put(CaseMapping.STRICT_RFC1459, new Pair<>("abcdwxyzABCDWXYZ!@#$%^&*(){}[];':,.<>", "abcdwxyzabcdwxyz!@#$%^&*(){}{};':,.<>"));
3838

3939
for (CaseMapping caseMapping : CaseMapping.values()) {
40-
Assert.assertTrue("Missing CaseMapping " + caseMapping.name(), test.containsKey(caseMapping));
40+
Assertions.assertTrue(test.containsKey(caseMapping), "Missing CaseMapping " + caseMapping.name());
4141
}
4242
for (Map.Entry<CaseMapping, Pair<String, String>> entry : test.entrySet()) {
43-
Assert.assertEquals("Incorrect lowercasing", entry.getKey().toLowerCase(entry.getValue().getLeft()), entry.getValue().getRight());
44-
Assert.assertTrue("Incorrect equalsIgnoreCase", entry.getKey().areEqualIgnoringCase(entry.getValue().getLeft(), entry.getValue().getRight()));
43+
Assertions.assertEquals(entry.getKey().toLowerCase(entry.getValue().getLeft()), entry.getValue().getRight(), "Incorrect lowercasing");
44+
Assertions.assertTrue(entry.getKey().areEqualIgnoringCase(entry.getValue().getLeft(), entry.getValue().getRight()), "Incorrect equalsIgnoreCase");
4545
}
4646
}
4747
}

src/test/java/org/kitteh/irc/client/library/DefaultBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.kitteh.irc.client.library;
22

3-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
44

55
/**
66
* Test building a builder.

src/test/java/org/kitteh/irc/client/library/FormatTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.kitteh.irc.client.library;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
55
import org.kitteh.irc.client.library.util.Format;
66

77
/**
@@ -13,31 +13,31 @@ public class FormatTest {
1313
*/
1414
@Test
1515
public void background() {
16-
Assert.assertEquals("\u000309,01", Format.GREEN.withBackground(Format.BLACK));
16+
Assertions.assertEquals("\u000309,01", Format.GREEN.withBackground(Format.BLACK));
1717
}
1818

1919
/**
2020
* Tests invalid background input.
2121
*/
22-
@Test(expected = IllegalArgumentException.class)
22+
@Test
2323
public void backgroundNonColorBackground() {
24-
Format.GREEN.withBackground(Format.BOLD);
24+
Assertions.assertThrows(IllegalArgumentException.class, () -> Format.GREEN.withBackground(Format.BOLD));
2525
}
2626

2727
/**
2828
* Tests invalid background input.
2929
*/
30-
@Test(expected = IllegalArgumentException.class)
30+
@Test
3131
public void backgroundNonColorForeground() {
32-
Format.BOLD.withBackground(Format.GREEN);
32+
Assertions.assertThrows(IllegalArgumentException.class, () -> Format.BOLD.withBackground(Format.GREEN));
3333
}
3434

3535
/**
3636
* Tests invalid background input.
3737
*/
38-
@Test(expected = IllegalArgumentException.class)
38+
@Test
3939
public void backgroundNull() {
40-
Format.GREEN.withBackground(null);
40+
Assertions.assertThrows(IllegalArgumentException.class, () -> Format.GREEN.withBackground(null));
4141
}
4242

4343
/**
@@ -47,7 +47,7 @@ public void backgroundNull() {
4747
public void format() {
4848
String colorChar = Format.COLOR_CHAR + "";
4949
for (Format format : Format.values()) {
50-
Assert.assertTrue("Color format with wrong length: " + format.name(), !format.toString().startsWith(colorChar) || (format.toString().length() == 3));
50+
Assertions.assertTrue(!format.toString().startsWith(colorChar) || (format.toString().length() == 3), "Color format with wrong length: " + format.name());
5151
}
5252
}
5353

@@ -56,17 +56,17 @@ public void format() {
5656
*/
5757
@Test
5858
public void stripColor() {
59-
Assert.assertEquals(Format.stripColor(Format.GREEN + "meow"), "meow");
60-
Assert.assertEquals(Format.stripColor(Format.BOLD + "purr"), Format.BOLD + "purr");
59+
Assertions.assertEquals(Format.stripColor(Format.GREEN + "meow"), "meow");
60+
Assertions.assertEquals(Format.stripColor(Format.BOLD + "purr"), Format.BOLD + "purr");
6161
}
6262

6363
/**
6464
* Tests format stripper.
6565
*/
6666
@Test
6767
public void stripFormat() {
68-
Assert.assertEquals(Format.stripFormatting(Format.GREEN + "meow"), Format.GREEN + "meow");
69-
Assert.assertEquals(Format.stripFormatting(Format.BOLD + "purr"), "purr");
68+
Assertions.assertEquals(Format.stripFormatting(Format.GREEN + "meow"), Format.GREEN + "meow");
69+
Assertions.assertEquals(Format.stripFormatting(Format.BOLD + "purr"), "purr");
7070
}
7171

7272
/**
@@ -76,9 +76,9 @@ public void stripFormat() {
7676
public void validColors() {
7777
for (Format format : Format.values()) {
7878
if (format.isColor()) {
79-
Assert.assertEquals("Invalid IRCFormat color char " + format.name(), (format.getColorChar() & 15), format.getColorChar());
79+
Assertions.assertEquals((format.getColorChar() & 15), format.getColorChar(), "Invalid IRCFormat color char " + format.name());
8080
} else {
81-
Assert.assertEquals("Invalid IRCFormat format " + format.name(), format.getColorChar(), -1);
81+
Assertions.assertEquals(format.getColorChar(), -1, "Invalid IRCFormat format " + format.name());
8282
}
8383
}
8484
}

src/test/java/org/kitteh/irc/client/library/command/AwayCommandTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.kitteh.irc.client.library.command;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
55
import org.kitteh.irc.client.library.Client;
66
import org.mockito.Mockito;
77

@@ -65,7 +65,7 @@ public void testToString() {
6565
awayCommand.away(MESSAGE);
6666
awayCommand.execute();
6767

68-
Assert.assertTrue(awayCommand.toString().contains(MESSAGE));
68+
Assertions.assertTrue(awayCommand.toString().contains(MESSAGE));
6969
}
7070

7171
/**

src/test/java/org/kitteh/irc/client/library/command/CapabilityRequestCommandTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.kitteh.irc.client.library.command;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
55
import org.kitteh.irc.client.library.Client;
66
import org.mockito.Mockito;
77

@@ -83,7 +83,7 @@ public void testToString() {
8383
sut.enable("testCapability");
8484

8585
// assert
86-
Assert.assertEquals("CapabilityRequestCommand (client=testClientToString, requests=[testCapability])", sut.toString());
86+
Assertions.assertEquals("CapabilityRequestCommand (client=testClientToString, requests=[testCapability])", sut.toString());
8787
}
8888

8989
/**

src/test/java/org/kitteh/irc/client/library/command/ChannelModeCommandTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.kitteh.irc.client.library.command;
22

33
import org.checkerframework.checker.nullness.qual.NonNull;
4-
import org.junit.Assert;
5-
import org.junit.Before;
6-
import org.junit.Test;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
77
import org.kitteh.irc.client.library.Client;
88
import org.kitteh.irc.client.library.element.Channel;
99
import org.kitteh.irc.client.library.element.ISupportParameter;
@@ -29,7 +29,7 @@ public class ChannelModeCommandTest {
2929
/**
3030
* And then Kitteh said, let there be test!
3131
*/
32-
@Before
32+
@BeforeEach
3333
public void before() {
3434
this.client = Mockito.mock(Client.class);
3535
Mockito.when(this.client.getChannel(CHANNEL)).thenReturn(Optional.of(Mockito.mock(Channel.class)));
@@ -47,7 +47,7 @@ public void testWithNoModeChanges() {
4747
sut.execute();
4848
Mockito.verify(this.client, Mockito.times(1)).sendRawLine("MODE " + CHANNEL);
4949

50-
Assert.assertFalse(sut.toString().isEmpty());
50+
Assertions.assertFalse(sut.toString().isEmpty());
5151
}
5252

5353
@Test
@@ -96,11 +96,11 @@ public void testWithFourParameterizedModeChanges() {
9696
inOrder.verify(this.client, Mockito.times(1)).sendRawLine("MODE " + CHANNEL + " +D meow");
9797
}
9898

99-
@Test(expected = IllegalArgumentException.class)
99+
@Test
100100
public void testWithOneSimpleModeChangeButWrongClient() {
101101
ChannelModeCommand sut = new ChannelModeCommand(this.client, CHANNEL);
102102
ChannelMode mode = this.getChannelMode('A', Mockito.mock(Client.class), ChannelMode.Type.A_MASK);
103-
sut.add(ModeStatus.Action.ADD, mode);
103+
Assertions.assertThrows(IllegalArgumentException.class, () -> sut.add(ModeStatus.Action.ADD, mode));
104104
}
105105

106106
@Test
@@ -148,14 +148,14 @@ public void testAddModeWithParameterViaUser() {
148148
Mockito.verify(this.client, Mockito.times(1)).sendRawLine("MODE " + CHANNEL + " +A kitteh");
149149
}
150150

151-
@Test(expected = IllegalArgumentException.class)
151+
@Test
152152
public void testAddModeWithParameterViaUserButWrongClient() {
153153
User userMock = Mockito.mock(User.class);
154154
Mockito.when(userMock.getClient()).thenReturn(Mockito.mock(Client.class));
155155
Mockito.when(userMock.getNick()).thenReturn("kitteh");
156156
ChannelModeCommand sut = new ChannelModeCommand(this.client, CHANNEL);
157157
ChannelUserMode mode = this.getChannelUserMode('A', this.client, ChannelMode.Type.B_PARAMETER_ALWAYS);
158-
sut.add(ModeStatus.Action.ADD, mode, userMock);
158+
Assertions.assertThrows(IllegalArgumentException.class, () -> sut.add(ModeStatus.Action.ADD, mode, userMock));
159159
}
160160

161161
@Test

src/test/java/org/kitteh/irc/client/library/command/KickCommandTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.kitteh.irc.client.library.command;
22

3-
import org.junit.Assert;
4-
import org.junit.Before;
5-
import org.junit.Test;
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66
import org.kitteh.irc.client.library.Client;
77
import org.kitteh.irc.client.library.defaults.feature.SimpleDefaultMessageMap;
88
import org.kitteh.irc.client.library.element.User;
@@ -23,7 +23,7 @@ public class KickCommandTest {
2323
/**
2424
* And then Kitteh said, let there be test!
2525
*/
26-
@Before
26+
@BeforeEach
2727
public void before() {
2828
this.client = Mockito.mock(Client.class);
2929
ServerInfo serverInfo = Mockito.mock(ServerInfo.class);
@@ -79,21 +79,21 @@ public void reasonElements() {
7979
/**
8080
* Tests a targetless execution.
8181
*/
82-
@Test(expected = IllegalStateException.class)
82+
@Test
8383
public void noTarget() {
8484
KickCommand command = new KickCommand(this.client, CHANNEL);
85-
command.execute();
85+
Assertions.assertThrowsExactly(IllegalStateException.class, command::execute);
8686
}
8787

8888
/**
8989
* Tests a wrong-Client attempt.
9090
*/
91-
@Test(expected = IllegalArgumentException.class)
91+
@Test
9292
public void wrongClientUser() {
9393
Mockito.when(this.user.getClient()).thenReturn(Mockito.mock(Client.class));
9494

9595
KickCommand command = new KickCommand(this.client, CHANNEL);
96-
command.target(this.user);
96+
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> command.target(this.user));
9797
}
9898

9999
/**
@@ -103,6 +103,6 @@ public void wrongClientUser() {
103103
public void toStringer() {
104104
KickCommand command = new KickCommand(this.client, CHANNEL);
105105

106-
Assert.assertTrue(command.toString().contains(CHANNEL));
106+
Assertions.assertTrue(command.toString().contains(CHANNEL));
107107
}
108108
}

0 commit comments

Comments
 (0)