Skip to content

Commit

Permalink
Migrate tests to modern JUnit Jupiter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mureinik authored and mbax committed Dec 28, 2023
1 parent 262113a commit 3e8590e
Show file tree
Hide file tree
Showing 39 changed files with 486 additions and 475 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.kitteh.irc.client.library;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.kitteh.irc.client.library.element.CapabilityState;
import org.kitteh.irc.client.library.feature.CapabilityManager;

Expand All @@ -23,11 +23,11 @@ public class CapabilityManagerTest {
@Test
public void testDefaultGetCapabilityMethodsInCapabilityManager() {
StubCapabilityManager sut = new StubCapabilityManager();
Assert.assertEquals(2, sut.getCapabilities().size());
Assert.assertTrue(sut.getCapability("Test1").isPresent());
Assert.assertTrue(sut.getSupportedCapability("Test1").isPresent());
Assert.assertFalse(sut.getSupportedCapability("Test2").isPresent());
Assert.assertFalse(sut.getCapability("Cats").isPresent());
Assertions.assertEquals(2, sut.getCapabilities().size());
Assertions.assertTrue(sut.getCapability("Test1").isPresent());
Assertions.assertTrue(sut.getSupportedCapability("Test1").isPresent());
Assertions.assertFalse(sut.getSupportedCapability("Test2").isPresent());
Assertions.assertFalse(sut.getCapability("Cats").isPresent());
}

/**
Expand All @@ -36,8 +36,8 @@ public void testDefaultGetCapabilityMethodsInCapabilityManager() {
@Test
public void testNativeCapabilityRetrieval() {
List<String> caps = CapabilityManager.Defaults.getDefaults();
Assert.assertFalse(caps.isEmpty());
Assert.assertTrue(caps.contains(CapabilityManager.Defaults.ACCOUNT_NOTIFY));
Assertions.assertFalse(caps.isEmpty());
Assertions.assertTrue(caps.contains(CapabilityManager.Defaults.ACCOUNT_NOTIFY));
}

/**
Expand All @@ -48,7 +48,7 @@ public void testNativeCapabilityRetrieval() {
@Test
public void testConstructorIsPrivate() throws Exception {
Constructor<CapabilityManager.Defaults> constructor = CapabilityManager.Defaults.class.getDeclaredConstructor();
Assert.assertTrue(Modifier.isPrivate(constructor.getModifiers()));
Assertions.assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/kitteh/irc/client/library/CaseMappingTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.kitteh.irc.client.library;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.kitteh.irc.client.library.feature.CaseMapping;
import org.kitteh.irc.client.library.util.Pair;

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

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

for (CaseMapping caseMapping : CaseMapping.values()) {
Assert.assertTrue("Missing CaseMapping " + caseMapping.name(), test.containsKey(caseMapping));
Assertions.assertTrue(test.containsKey(caseMapping), "Missing CaseMapping " + caseMapping.name());
}
for (Map.Entry<CaseMapping, Pair<String, String>> entry : test.entrySet()) {
Assert.assertEquals("Incorrect lowercasing", entry.getKey().toLowerCase(entry.getValue().getLeft()), entry.getValue().getRight());
Assert.assertTrue("Incorrect equalsIgnoreCase", entry.getKey().areEqualIgnoringCase(entry.getValue().getLeft(), entry.getValue().getRight()));
Assertions.assertEquals(entry.getKey().toLowerCase(entry.getValue().getLeft()), entry.getValue().getRight(), "Incorrect lowercasing");
Assertions.assertTrue(entry.getKey().areEqualIgnoringCase(entry.getValue().getLeft(), entry.getValue().getRight()), "Incorrect equalsIgnoreCase");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.kitteh.irc.client.library;

import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Test building a builder.
Expand Down
32 changes: 16 additions & 16 deletions src/test/java/org/kitteh/irc/client/library/FormatTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.kitteh.irc.client.library;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.kitteh.irc.client.library.util.Format;

/**
Expand All @@ -13,31 +13,31 @@ public class FormatTest {
*/
@Test
public void background() {
Assert.assertEquals("\u000309,01", Format.GREEN.withBackground(Format.BLACK));
Assertions.assertEquals("\u000309,01", Format.GREEN.withBackground(Format.BLACK));
}

/**
* Tests invalid background input.
*/
@Test(expected = IllegalArgumentException.class)
@Test
public void backgroundNonColorBackground() {
Format.GREEN.withBackground(Format.BOLD);
Assertions.assertThrows(IllegalArgumentException.class, () -> Format.GREEN.withBackground(Format.BOLD));
}

/**
* Tests invalid background input.
*/
@Test(expected = IllegalArgumentException.class)
@Test
public void backgroundNonColorForeground() {
Format.BOLD.withBackground(Format.GREEN);
Assertions.assertThrows(IllegalArgumentException.class, () -> Format.BOLD.withBackground(Format.GREEN));
}

/**
* Tests invalid background input.
*/
@Test(expected = IllegalArgumentException.class)
@Test
public void backgroundNull() {
Format.GREEN.withBackground(null);
Assertions.assertThrows(IllegalArgumentException.class, () -> Format.GREEN.withBackground(null));
}

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

Expand All @@ -56,17 +56,17 @@ public void format() {
*/
@Test
public void stripColor() {
Assert.assertEquals(Format.stripColor(Format.GREEN + "meow"), "meow");
Assert.assertEquals(Format.stripColor(Format.BOLD + "purr"), Format.BOLD + "purr");
Assertions.assertEquals(Format.stripColor(Format.GREEN + "meow"), "meow");
Assertions.assertEquals(Format.stripColor(Format.BOLD + "purr"), Format.BOLD + "purr");
}

/**
* Tests format stripper.
*/
@Test
public void stripFormat() {
Assert.assertEquals(Format.stripFormatting(Format.GREEN + "meow"), Format.GREEN + "meow");
Assert.assertEquals(Format.stripFormatting(Format.BOLD + "purr"), "purr");
Assertions.assertEquals(Format.stripFormatting(Format.GREEN + "meow"), Format.GREEN + "meow");
Assertions.assertEquals(Format.stripFormatting(Format.BOLD + "purr"), "purr");
}

/**
Expand All @@ -76,9 +76,9 @@ public void stripFormat() {
public void validColors() {
for (Format format : Format.values()) {
if (format.isColor()) {
Assert.assertEquals("Invalid IRCFormat color char " + format.name(), (format.getColorChar() & 15), format.getColorChar());
Assertions.assertEquals((format.getColorChar() & 15), format.getColorChar(), "Invalid IRCFormat color char " + format.name());
} else {
Assert.assertEquals("Invalid IRCFormat format " + format.name(), format.getColorChar(), -1);
Assertions.assertEquals(format.getColorChar(), -1, "Invalid IRCFormat format " + format.name());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.kitteh.irc.client.library.command;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.kitteh.irc.client.library.Client;
import org.mockito.Mockito;

Expand Down Expand Up @@ -65,7 +65,7 @@ public void testToString() {
awayCommand.away(MESSAGE);
awayCommand.execute();

Assert.assertTrue(awayCommand.toString().contains(MESSAGE));
Assertions.assertTrue(awayCommand.toString().contains(MESSAGE));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.kitteh.irc.client.library.command;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.kitteh.irc.client.library.Client;
import org.mockito.Mockito;

Expand Down Expand Up @@ -83,7 +83,7 @@ public void testToString() {
sut.enable("testCapability");

// assert
Assert.assertEquals("CapabilityRequestCommand (client=testClientToString, requests=[testCapability])", sut.toString());
Assertions.assertEquals("CapabilityRequestCommand (client=testClientToString, requests=[testCapability])", sut.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.kitteh.irc.client.library.command;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kitteh.irc.client.library.Client;
import org.kitteh.irc.client.library.element.Channel;
import org.kitteh.irc.client.library.element.ISupportParameter;
Expand All @@ -29,7 +29,7 @@ public class ChannelModeCommandTest {
/**
* And then Kitteh said, let there be test!
*/
@Before
@BeforeEach
public void before() {
this.client = Mockito.mock(Client.class);
Mockito.when(this.client.getChannel(CHANNEL)).thenReturn(Optional.of(Mockito.mock(Channel.class)));
Expand All @@ -47,7 +47,7 @@ public void testWithNoModeChanges() {
sut.execute();
Mockito.verify(this.client, Mockito.times(1)).sendRawLine("MODE " + CHANNEL);

Assert.assertFalse(sut.toString().isEmpty());
Assertions.assertFalse(sut.toString().isEmpty());
}

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

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

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

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

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.kitteh.irc.client.library.command;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kitteh.irc.client.library.Client;
import org.kitteh.irc.client.library.defaults.feature.SimpleDefaultMessageMap;
import org.kitteh.irc.client.library.element.User;
Expand All @@ -23,7 +23,7 @@ public class KickCommandTest {
/**
* And then Kitteh said, let there be test!
*/
@Before
@BeforeEach
public void before() {
this.client = Mockito.mock(Client.class);
ServerInfo serverInfo = Mockito.mock(ServerInfo.class);
Expand Down Expand Up @@ -79,21 +79,21 @@ public void reasonElements() {
/**
* Tests a targetless execution.
*/
@Test(expected = IllegalStateException.class)
@Test
public void noTarget() {
KickCommand command = new KickCommand(this.client, CHANNEL);
command.execute();
Assertions.assertThrowsExactly(IllegalStateException.class, command::execute);
}

/**
* Tests a wrong-Client attempt.
*/
@Test(expected = IllegalArgumentException.class)
@Test
public void wrongClientUser() {
Mockito.when(this.user.getClient()).thenReturn(Mockito.mock(Client.class));

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

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

Assert.assertTrue(command.toString().contains(CHANNEL));
Assertions.assertTrue(command.toString().contains(CHANNEL));
}
}
Loading

0 comments on commit 3e8590e

Please sign in to comment.