Skip to content

Introduce JUnit5 #2493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 22, 2017
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ addons:
- python3-yaml
install: ./.travis/setup_lobby_database
before_script: ./.travis/setup_gpg
script: ./gradlew check jacocoTestReport -x validateYamls
script: ./gradlew check jacocoJunit5TestReport -x validateYamls
after_success:
- ./.travis/update_checkstyle_thresholds
- bash <(curl -s https://codecov.io/bash) # upload coverage report - https://github.com/codecov/example-gradle
Expand Down
58 changes: 41 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
}
}

plugins {
id 'java'
id 'application'
Expand All @@ -11,6 +20,7 @@ plugins {
}

apply from: 'gradle/scripts/yaml.gradle'
apply plugin: 'org.junit.platform.gradle.plugin'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, but at least the JUnit team is aware of the issue: junit-team/junit5#768. However, it appears to have been pushed back to 5.1 as of 14 hours ago. 😞

Note that there's a workaround listed in the issue, but I'm not sure if it's worth introducing that just to keep build.gradle clean.

Nit: Pasting the buildscript configuration from the JUnit docs introduced mixed tabs/spaces into this file.


group = 'triplea'
description = 'TripleA is a free online turn based strategy game and board game engine, similar to such board games as Axis & Allies or Risk.'
Expand Down Expand Up @@ -49,7 +59,7 @@ sourceSets {
java.srcDir 'src/integ_test/java'
resources.srcDir 'src/integ_test/resources'

compileClasspath = sourceSets.main.output + sourceSets.test.output + configurations.testRuntime
compileClasspath = sourceSets.main.output + sourceSets.test.output + configurations.testRuntime + configurations.junitPlatform
runtimeClasspath = output + compileClasspath
}
}
Expand Down Expand Up @@ -95,28 +105,22 @@ dependencies {
testCompile 'eu.codearte.catch-exception:catch-exception:2.0.0-ALPHA-1'
testCompile 'nl.jqno.equalsverifier:equalsverifier:2.3.3'
testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'
testCompile 'org.mockito:mockito-core:2.10.0'
testCompile 'junit:junit:4.12'
}

test {
exclude '**/*Tests.class'
testCompile 'org.mockito:mockito-core:2.11.0'
testRuntime 'org.junit.platform:junit-platform-launcher:1.0.1'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could you please move this down one line so we keep the different configurations grouped together? It's easy to miss the difference between testCompile and testRuntime when scanning the list. In fact, maybe an extra line of whitespace between them would be helpful, just as we did between compile and testCompile above.

testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.1'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.0.1'
}


task integTest(type: Test) {
task integTest(type: JavaExec) {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = 'Runs the integration tests.'

testClassesDirs = sourceSets.integTest.output.classesDirs
classpath = sourceSets.integTest.runtimeClasspath

binResultsDir = file("$buildDir/integration_test_results/binary/integTest")

reports {
reports.html.destination = file("${reports.html.destination}/$name")
reports.junitXml.destination = file("${reports.junitXml.destination}/$name")
}
main = 'org.junit.platform.console.ConsoleLauncher'
args '--details', 'none'
args "--reports-dir=$project.buildDir/integ-test-results/junit-platform"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The project. qualifier is superfluous here; $buildDir should be sufficient and is consistent with the rest of the build script.

args '--scan-classpath'
args '--include-classname', '^.*IntegrationTests?$'

mustRunAfter tasks.test
}
Expand Down Expand Up @@ -313,3 +317,23 @@ jacocoTestReport {
html.enabled true
}
}

afterEvaluate {
def junitPlatformTest = tasks.junitPlatformTest

jacoco {
applyTo(junitPlatformTest)
}

task jacocoJunit5TestReport(type: JacocoReport) {
executionData junitPlatformTest
sourceSets sourceSets.main
sourceDirectories = files(sourceSets.main.allSource.srcDirs)
classDirectories = files(sourceSets.main.output)

reports {
xml.enabled true
html.enabled true
}
}
}
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
<property name="allowMissingThrowsTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
<property name="minLineCount" value="2"/>
<property name="allowedAnnotations" value="Override, Test, BeforeClass, AfterClass, Before, After"/>
<property name="allowedAnnotations" value="Override, Test, BeforeAll, AfterAll, BeforeEach, AfterEach"/>
<property name="allowThrowsTagsForSubclasses" value="true"/>
<property name="tokens" value="METHOD_DEF"/>
</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import static org.hamcrest.core.Is.is;

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

public class ClientContextIntegrationTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package games.strategy.engine.chat;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

import java.time.Duration;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import games.strategy.engine.lobby.server.NullModeratorController;
import games.strategy.engine.message.ChannelMessenger;
Expand Down Expand Up @@ -46,7 +48,7 @@ public final class ChatIntegrationTest {
private final TestChatListener client2ChatListener = new TestChatListener();
private NullModeratorController serverModeratorController;

@Before
@BeforeEach
public void setUp() throws Exception {
serverMessenger = new ServerMessenger("Server", serverPort);
serverMessenger.setAcceptNewConnections(true);
Expand All @@ -66,7 +68,7 @@ public void setUp() throws Exception {
serverModeratorController.register(serverRemoteMessenger);
}

@After
@AfterEach
public void tearDown() {
if (serverMessenger != null) {
serverMessenger.shutDown();
Expand All @@ -79,28 +81,30 @@ public void tearDown() {
}
}

@Test(timeout = 15_000)
public void shouldBeAbleToChatAcrossMultipleNodes() throws Exception {
final ChatController controller = newChatController();
final Chat server = newChat(serverMessenger, serverChannelMessenger, serverRemoteMessenger);
server.addChatListener(serverChatListener);
final Chat client1 = newChat(client1Messenger, client1ChannelMessenger, client1RemoteMessenger);
client1.addChatListener(client1ChatListener);
final Chat client2 = newChat(client2Messenger, client2ChannelMessenger, client2RemoteMessenger);
client2.addChatListener(client2ChatListener);
waitFor(this::allNodesToConnect);

sendMessagesFrom(client2);
sendMessagesFrom(server);
sendMessagesFrom(client1);
waitFor(this::allMessagesToArrive);

client1.shutdown();
client2.shutdown();
waitFor(this::clientNodesToDisconnect);

controller.deactivate();
waitFor(this::serverNodeToDisconnect);
@Test
public void shouldBeAbleToChatAcrossMultipleNodes() {
assertTimeoutPreemptively(Duration.ofSeconds(15), () -> {
final ChatController controller = newChatController();
final Chat server = newChat(serverMessenger, serverChannelMessenger, serverRemoteMessenger);
server.addChatListener(serverChatListener);
final Chat client1 = newChat(client1Messenger, client1ChannelMessenger, client1RemoteMessenger);
client1.addChatListener(client1ChatListener);
final Chat client2 = newChat(client2Messenger, client2ChannelMessenger, client2RemoteMessenger);
client2.addChatListener(client2ChatListener);
waitFor(this::allNodesToConnect);

sendMessagesFrom(client2);
sendMessagesFrom(server);
sendMessagesFrom(client1);
waitFor(this::allMessagesToArrive);

client1.shutdown();
client2.shutdown();
waitFor(this::clientNodesToDisconnect);

controller.deactivate();
waitFor(this::serverNodeToDisconnect);
});
}

private ChatController newChatController() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;

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

public class LobbyServerPropertiesFetcherIntegrationTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;

import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import games.strategy.net.ClientMessenger;
import games.strategy.net.CouldNotLogInException;
Expand All @@ -31,7 +31,7 @@ public final class ClientLoginIntegrationTest {
private IServerMessenger serverMessenger;
private final int serverPort = TestUtil.getUniquePort();

@Before
@BeforeEach
public void setUp() throws Exception {
serverMessenger = newServerMessenger();
}
Expand All @@ -49,7 +49,7 @@ private static ILoginValidator newLoginValidator(final IServerMessenger serverMe
return clientLoginValidator;
}

@After
@AfterEach
public void tearDown() {
serverMessenger.shutDown();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package games.strategy.engine.lobby.server;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -11,8 +11,8 @@
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand All @@ -33,7 +33,7 @@ public class ModeratorControllerIntegrationTest {
private ConnectionChangeListener connectionChangeListener;
private INode adminNode;

@Before
@BeforeEach
public void setUp() throws UnknownHostException {
moderatorController = new ModeratorController(serverMessenger, null);
final String adminName = Util.createUniqueTimeStamp();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package games.strategy.engine.lobby.server.db;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.Connection;
import java.sql.PreparedStatement;

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

import games.strategy.util.Util;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package games.strategy.engine.lobby.server.db;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.sql.Timestamp;
import java.time.Instant;

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

import games.strategy.util.MD5Crypt;
import games.strategy.util.Tuple;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package games.strategy.engine.lobby.server.db;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.sql.Timestamp;
import java.time.Instant;

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

import games.strategy.util.Tuple;
import games.strategy.util.Util;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.google.common.base.Strings;

Expand All @@ -22,7 +22,7 @@ public class EmailLimitIntegrationTest {

private static Connection connection;

@BeforeClass
@BeforeAll
public static void setup() throws SQLException {
connection = Database.getPostgresConnection();
connection.setAutoCommit(true);
Expand Down Expand Up @@ -54,7 +54,7 @@ private static void createAccountWithEmail(final String email) {
}
}

@AfterClass
@AfterAll
public static void tearDown() throws SQLException {
connection.close();
}
Expand Down
Loading