BAEL-6812 Test Main Method with JUnit

- fixed review comments
This commit is contained in:
Tetiana Okhotnik 2023-09-03 23:00:52 +03:00
parent fd6b6aa6d5
commit 2f639b9aab
6 changed files with 2 additions and 19 deletions

View File

@ -12,28 +12,23 @@
<artifactId>junit-5-basics-2</artifactId> <artifactId>junit-5-basics-2</artifactId>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency> <dependency>
<groupId>commons-cli</groupId> <groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId> <artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version> <version>${commons-cli.version}</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter-api.version}</version> <version>${junit-jupiter-api.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version> <version>${mockito-core.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
@ -54,7 +49,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<commons-cli.version>1.5.0</commons-cli.version> <commons-cli.version>1.5.0</commons-cli.version>
<junit-jupiter-api.version>5.10.0</junit-jupiter-api.version> <junit-jupiter-api.version>5.10.0</junit-jupiter-api.version>
<mockito-core.version>5.4.0</mockito-core.version> <mockito-core.version>5.5.0</mockito-core.version>
</properties> </properties>
</project> </project>

View File

@ -55,7 +55,6 @@ public class Bootstrapper {
.build(); .build();
Options options = new Options(); Options options = new Options();
options.addOption(inputTypeOption); options.addOption(inputTypeOption);
options.addOption(fileNameOption); options.addOption(fileNameOption);
return options; return options;

View File

@ -21,9 +21,7 @@ public class InputReader {
private String readFromConsole() { private String readFromConsole() {
System.out.println("Enter values for calculation: \n"); System.out.println("Enter values for calculation: \n");
String str = new Scanner(System.in).nextLine(); return new Scanner(System.in).nextLine();
return str;
} }
private String readFromFile(String fileName) { private String readFromFile(String fileName) {

View File

@ -17,7 +17,6 @@ import org.apache.commons.cli.ParseException;
public class StaticMain { public class StaticMain {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Received input parameters: " + Arrays.asList(args)); System.out.println("Received input parameters: " + Arrays.asList(args));
processRequest(args); processRequest(args);
@ -54,7 +53,6 @@ public class StaticMain {
} }
public static Options getOptions() { public static Options getOptions() {
Option inputTypeOption = Option.builder("i") Option inputTypeOption = Option.builder("i")
.longOpt("input") .longOpt("input")
.required(true) .required(true)
@ -70,7 +68,6 @@ public class StaticMain {
.build(); .build();
Options options = new Options(); Options options = new Options();
options.addOption(inputTypeOption); options.addOption(inputTypeOption);
options.addOption(fileNameOption); options.addOption(fileNameOption);
return options; return options;

View File

@ -40,10 +40,8 @@ public class TestSimpleMain {
System.setIn(fips); System.setIn(fips);
System.setOut(out); System.setOut(out);
//execute
SimpleMain.main(arguments); SimpleMain.main(arguments);
//verify
String consoleOutput = byteArrayOutputStream.toString(Charset.defaultCharset()); String consoleOutput = byteArrayOutputStream.toString(Charset.defaultCharset());
assertTrue(consoleOutput.contains("Calculated sum: 6")); assertTrue(consoleOutput.contains("Calculated sum: 6"));
@ -63,10 +61,8 @@ public class TestSimpleMain {
System.setIn(fips); System.setIn(fips);
System.setOut(out); System.setOut(out);
//execute
SimpleMain.main(arguments); SimpleMain.main(arguments);
//verify
String consoleOutput = byteArrayOutputStream.toString(Charset.defaultCharset()); String consoleOutput = byteArrayOutputStream.toString(Charset.defaultCharset());
assertTrue(consoleOutput.contains("Calculated sum: 10")); assertTrue(consoleOutput.contains("Calculated sum: 10"));

View File

@ -24,10 +24,8 @@ public class TestStaticMain {
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
//execute
StaticMain.main(arguments); StaticMain.main(arguments);
//verify
mockedStatic.verify(() -> StaticMain.calculateSum(stringArgumentCaptor.capture())); mockedStatic.verify(() -> StaticMain.calculateSum(stringArgumentCaptor.capture()));
System.setIn(original); System.setIn(original);