Skip to content

Commit 84f5672

Browse files
committed
Improving Unit tests
1 parent a9f9bfb commit 84f5672

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/main/java/org/contextmapper/cli/ContextMapperCLI.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ public void run() {
2323
System.out.println("Context Mapper CLI. Use 'cm --help' for usage information.");
2424
}
2525

26+
public static int runCLI(String[] args) {
27+
return new CommandLine(new ContextMapperCLI()).execute(args);
28+
}
29+
2630
public static void main(String[] args) {
27-
CommandLine commandLine = new CommandLine(new ContextMapperCLI());
28-
int exitCode = commandLine.execute(args);
31+
int exitCode = runCLI(args);
2932
System.exit(exitCode);
3033
}
3134
}

src/test/java/org/contextmapper/cli/ContextMapperCLITest.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.api.DisplayName;
7-
import picocli.CommandLine;
87

98
import java.io.ByteArrayOutputStream;
109
import java.io.PrintStream;
@@ -17,13 +16,11 @@ class ContextMapperCLITest {
1716
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
1817
private final PrintStream originalOut = System.out;
1918
private final PrintStream originalErr = System.err;
20-
private CommandLine cmd;
2119

2220
@BeforeEach
2321
void setUp() {
2422
System.setOut(new PrintStream(outContent));
2523
System.setErr(new PrintStream(errContent));
26-
cmd = new CommandLine(new ContextMapperCLI());
2724
}
2825

2926
@AfterEach
@@ -39,7 +36,7 @@ void main_WhenCalledWithoutCommand_ThenPrintsTopLevelHelp() {
3936
// No arguments for this test case
4037

4138
// When
42-
cmd.execute();
39+
ContextMapperCLI.runCLI(new String[0]);
4340

4441
// Then
4542
assertThat(outContent.toString()).contains("Context Mapper CLI. Use 'cm --help' for usage information.");
@@ -52,7 +49,7 @@ void main_WhenCalledWithHelpOption_ThenPrintsTopLevelHelp() {
5249
String[] args = { "--help" };
5350

5451
// When
55-
int exitCode = cmd.execute(args);
52+
int exitCode = ContextMapperCLI.runCLI(args);
5653

5754
// Then
5855
assertThat(exitCode).isEqualTo(0);
@@ -70,11 +67,25 @@ void main_WhenCalledWithVersionOption_ThenPrintsVersion() {
7067
String[] args = { "--version" };
7168

7269
// When
73-
int exitCode = cmd.execute(args);
70+
int exitCode = ContextMapperCLI.runCLI(args);
7471

7572
// Then
7673
assertThat(exitCode).isEqualTo(0);
77-
assertThat(outContent.toString()).contains("Context Mapper CLI");
74+
assertThat(outContent.toString().trim()).isEqualTo("Context Mapper CLI DEVELOPMENT VERSION");
75+
}
76+
77+
@Test
78+
@DisplayName("main() should print version when called with -V option")
79+
void main_WhenCalledWithShortVersionOption_ThenPrintsVersion() {
80+
// Given
81+
String[] args = { "-V" };
82+
83+
// When
84+
int exitCode = ContextMapperCLI.runCLI(args);
85+
86+
// Then
87+
assertThat(exitCode).isEqualTo(0);
88+
assertThat(outContent.toString().trim()).isEqualTo("Context Mapper CLI DEVELOPMENT VERSION");
7889
}
7990

8091
@Test
@@ -84,7 +95,7 @@ void main_WhenCalledWithInvalidOption_ThenPrintsErrorAndUsage() {
8495
String[] args = { "--invalid-option" };
8596

8697
// When
87-
int exitCode = cmd.execute(args);
98+
int exitCode = ContextMapperCLI.runCLI(args);
8899

89100
// Then
90101
assertThat(exitCode).isNotEqualTo(0);
@@ -100,7 +111,7 @@ void main_WhenCalledWithInvalidSubcommand_ThenPrintsErrorAndUsage() {
100111
String[] args = { "invalid-command" };
101112

102113
// When
103-
int exitCode = cmd.execute(args);
114+
int exitCode = ContextMapperCLI.runCLI(args);
104115

105116
// Then
106117
assertThat(exitCode).isNotEqualTo(0);

0 commit comments

Comments
 (0)