Skip to content

Commit

Permalink
Fix tests for Java 21 > avoid System.exit() in tests
Browse files Browse the repository at this point in the history
remove dependency com.ginsberg:junit5-system-exit
  • Loading branch information
SgtSilvio committed Mar 8, 2024
1 parent 15bb2d5 commit 1409757
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 43 deletions.
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ testing {

val test by getting(JvmTestSuite::class) {
dependencies {
implementation(libs.junit.systemExit)
implementation(libs.mockito)
implementation(libs.okhttp.mockWebserver)
}
Expand All @@ -244,7 +243,6 @@ testing {
dependencies {
implementation(libs.awaitility)
implementation(libs.hivemq.testcontainer.junit5)
implementation(libs.junit.systemExit)
implementation(libs.mockito)
implementation(libs.testcontainers)

Expand Down
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jsr305 = "3.0.2"
junit-jupiter = "5.10.2"
junit-pioneer = "2.2.0"
junit-platformLauncher = "1.10.2"
junit-systemExit = "1.1.2"
mockito = "5.10.0"
netty = "4.1.107.Final"
okhttp = "4.12.0"
Expand Down Expand Up @@ -57,7 +56,6 @@ jsr305 = { module = "com.google.code.findbugs:jsr305", version.ref = "jsr305" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
junit-pioneer = { module = "org.junit-pioneer:junit-pioneer", version.ref = "junit-pioneer" }
junit-platformLauncher = { module = "org.junit.platform:junit-platform-launcher", version.ref = "junit-platformLauncher" }
junit-systemExit = { module = "com.ginsberg:junit5-system-exit", version.ref = "junit-systemExit" }
mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }
netty-codec-http = { module = "io.netty:netty-codec-http", version.ref = "netty" }
netty-handler = { module = "io.netty:netty-handler", version.ref = "netty" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.hivemq.cli.commands.cli;

import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.utils.TestLoggerUtils;
import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension;
Expand All @@ -28,6 +27,8 @@
import org.junit.jupiter.api.Test;
import org.testcontainers.utility.DockerImageName;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Disabled("Tests are only used to check output")
class TestBrokerCommandDefaultIT {

Expand All @@ -50,26 +51,23 @@ static void afterAll() {
}

@Test
@ExpectSystemExitWithStatus(0)
void mqtt3_failed_connect() {
MqttCLIMain.main("test", "-V", "3");
assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3"));
}

@Test
@ExpectSystemExitWithStatus(0)
void mqtt5_failed_connect() {
MqttCLIMain.main("test", "-V", "5");
assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "5"));
}

@Test
@ExpectSystemExitWithStatus(0)
void mqtt3_features() {
MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())));
}

@Test
@ExpectSystemExitWithStatus(0)
void mqtt5_features() {
MqttCLIMain.main("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0,
MqttCLIMain.mainWithExitCode("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.hivemq.cli.commands.cli;

import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.utils.TestLoggerUtils;
import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension;
Expand All @@ -29,6 +28,8 @@
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Disabled("Tests are only used to check output")
class TestBrokerCommandQos0IT {

Expand All @@ -52,14 +53,12 @@ static void afterAll() {
}

@Test
@ExpectSystemExitWithStatus(0)
void qos0_restricted_mqtt3_features() {
MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())));
}

@Test
@ExpectSystemExitWithStatus(0)
void qos0_restricted_mqtt5_features() {
MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.hivemq.cli.commands.cli;

import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.utils.TestLoggerUtils;
import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension;
Expand All @@ -29,6 +28,8 @@
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Disabled("Tests are only used to check output")
class TestBrokerCommandQos1IT {

Expand All @@ -52,14 +53,13 @@ static void afterAll() {
}

@Test
@ExpectSystemExitWithStatus(0)
void qos1_restricted_mqtt3_features() {
MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())));
}

@Test
@ExpectSystemExitWithStatus(0)
void qos1_restricted_mqtt5_features() {
MqttCLIMain.main("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0,
MqttCLIMain.mainWithExitCode("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.hivemq.cli.commands.cli;

import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.hivemq.cli.MqttCLIMain;
import com.hivemq.cli.utils.TestLoggerUtils;
import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension;
Expand All @@ -29,6 +28,8 @@
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Disabled("Tests are only used to check output")
class TestBrokerCommandRestrictedIT {

Expand All @@ -52,14 +53,13 @@ static void afterAll() {
}

@Test
@ExpectSystemExitWithStatus(0)
void restricted_mqtt3_features() {
MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())));
}

@Test
@ExpectSystemExitWithStatus(0)
void restricted_mqtt5_features() {
MqttCLIMain.main("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort()));
assertEquals(0,
MqttCLIMain.mainWithExitCode("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort())));
}
}
13 changes: 7 additions & 6 deletions src/main/java/com/hivemq/cli/MqttCLIMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class MqttCLIMain {
public static @Nullable MqttCLI MQTT_CLI = null;

public static void main(final @NotNull String... args) {
System.exit(mainWithExitCode(args));
}

public static int mainWithExitCode(final @NotNull String... args) {

Security.setProperty("crypto.policy", "unlimited");

Expand All @@ -50,20 +54,17 @@ public static void main(final @NotNull String... args) {
defaultCLIProperties.init();
} catch (final Exception e) {
System.err.println(e.getMessage());
System.exit(1);
return 1;
}

if (args.length == 0) {
System.out.println(commandLine.getUsageMessage());
System.exit(0);
return 0;
}

Runtime.getRuntime().addShutdownHook(new DisconnectAllClientsTask());

final int exitCode = commandLine.execute(args);

System.exit(exitCode);

return commandLine.execute(args);
}

private static class DisconnectAllClientsTask extends Thread {
Expand Down
15 changes: 6 additions & 9 deletions src/test/java/com/hivemq/cli/MqttCLIMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
*/
package com.hivemq.cli;

import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import com.hivemq.cli.utils.TestLoggerUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class MqttCLIMainTest {

@BeforeEach
Expand All @@ -34,26 +35,22 @@ void tearDown() {
}

@Test
@ExpectSystemExitWithStatus(0)
void mqtt_command() {
MqttCLIMain.main();
assertEquals(0, MqttCLIMain.mainWithExitCode());
}

@Test
@ExpectSystemExitWithStatus(0)
void hivemq_command() {
MqttCLIMain.main("hivemq");
assertEquals(0, MqttCLIMain.mainWithExitCode("hivemq"));
}

@Test
@ExpectSystemExitWithStatus(0)
void hivemq_export_command() {
MqttCLIMain.main("hivemq", "export");
assertEquals(0, MqttCLIMain.mainWithExitCode("hivemq", "export"));
}

@Test
@ExpectSystemExitWithStatus(0)
void hivemq_export_clients_help_command() {
MqttCLIMain.main("hivemq", "export", "clients", "-h");
assertEquals(0, MqttCLIMain.mainWithExitCode("hivemq", "export", "clients", "-h"));
}
}

0 comments on commit 1409757

Please sign in to comment.