BAEL-6812 Test Main Method with JUnit
- added sample to test main method with different architecture
This commit is contained in:
+100
@@ -0,0 +1,100 @@
|
||||
package com.baeldung.junit.main.test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestSimpleMain {
|
||||
private InputStream defaultIn;
|
||||
private PrintStream defaultOut;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
defaultIn = System.in;
|
||||
defaultOut = System.out;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
System.setIn(defaultIn);
|
||||
System.setOut(defaultOut);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArgumentAsConsoleInput_WhenReadFromSubstitutedByteStream_ThenSuccessfullyCalculate() throws IOException {
|
||||
String[] arguments = new String[] { "-i", "CONSOLE" };
|
||||
|
||||
final InputStream fips = new ByteArrayInputStream("1 2 3".getBytes());
|
||||
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
final PrintStream out = new PrintStream(byteArrayOutputStream);
|
||||
|
||||
System.setIn(fips);
|
||||
System.setOut(out);
|
||||
|
||||
//execute
|
||||
SimpleMain.main(arguments);
|
||||
|
||||
//verify
|
||||
String consoleOutput = byteArrayOutputStream.toString(Charset.defaultCharset());
|
||||
assertTrue(consoleOutput.contains("Calculated sum: 6"));
|
||||
|
||||
fips.close();
|
||||
out.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArgumentAsConsoleInput_WhenReadFromSubstitutedFileStream_ThenSuccessfullyCalculate() throws IOException {
|
||||
String[] arguments = new String[] { "-i", "CONSOLE" };
|
||||
|
||||
final InputStream fips = getClass().getClassLoader()
|
||||
.getResourceAsStream("test-input.txt");
|
||||
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
final PrintStream out = new PrintStream(byteArrayOutputStream);
|
||||
|
||||
System.setIn(fips);
|
||||
System.setOut(out);
|
||||
|
||||
//execute
|
||||
SimpleMain.main(arguments);
|
||||
|
||||
//verify
|
||||
String consoleOutput = byteArrayOutputStream.toString(Charset.defaultCharset());
|
||||
assertTrue(consoleOutput.contains("Calculated sum: 10"));
|
||||
|
||||
fips.close();
|
||||
out.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLongArgumentAsConsoleInput_WhenReadFromSubstitutedFileStream_ThenSuccessfullyCalculate() throws IOException {
|
||||
String[] arguments = new String[] { "--input", "CONSOLE" };
|
||||
|
||||
final InputStream fips = getClass().getClassLoader()
|
||||
.getResourceAsStream("test-input.txt");
|
||||
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
final PrintStream out = new PrintStream(byteArrayOutputStream);
|
||||
|
||||
System.setIn(fips);
|
||||
System.setOut(out);
|
||||
|
||||
//execute
|
||||
SimpleMain.main(arguments);
|
||||
|
||||
//verify
|
||||
String consoleOutput = byteArrayOutputStream.toString(Charset.defaultCharset());
|
||||
assertTrue(consoleOutput.contains("Calculated sum: 10"));
|
||||
|
||||
fips.close();
|
||||
out.close();
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.junit.main.test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
public class TestStaticMain {
|
||||
|
||||
@Test
|
||||
public void givenArgumentAsConsoleInput_WhenReadFromSubstitutedByteArrayInputStream_ThenSuccessfullyCalculate() throws IOException {
|
||||
String[] arguments = new String[] { "-i", "CONSOLE" };
|
||||
try (MockedStatic<StaticMain> mockedStatic = Mockito.mockStatic(StaticMain.class, Mockito.CALLS_REAL_METHODS);
|
||||
InputStream fips = new ByteArrayInputStream("1 2 3".getBytes())) {
|
||||
|
||||
final InputStream original = System.in;
|
||||
|
||||
//Reassigns the "standard" input stream
|
||||
System.setIn(fips);
|
||||
|
||||
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
|
||||
|
||||
//execute
|
||||
StaticMain.main(arguments);
|
||||
|
||||
//verify
|
||||
mockedStatic.verify(() -> StaticMain.calculateSum(stringArgumentCaptor.capture()));
|
||||
|
||||
System.setIn(original);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
4 5 1
|
||||
Reference in New Issue
Block a user