https://github.com/drop-project-edu/stdin-stdout-junit-helper
Helper for testing stdin/stdout interactions
https://github.com/drop-project-edu/stdin-stdout-junit-helper
java junit kotlin stdin stdout
Last synced: 5 months ago
JSON representation
Helper for testing stdin/stdout interactions
- Host: GitHub
- URL: https://github.com/drop-project-edu/stdin-stdout-junit-helper
- Owner: drop-project-edu
- License: apache-2.0
- Created: 2020-04-09T18:40:35.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-11-04T14:13:44.000Z (over 1 year ago)
- Last Synced: 2024-11-04T15:24:14.379Z (over 1 year ago)
- Topics: java, junit, kotlin, stdin, stdout
- Language: Java
- Size: 26.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Stdin/Stdout Helper for JUnit
Useful for testing java/kotlin programs interaction with the command line.
## How to install
Include the following dependency on your pom file:
org.dropproject
stdin-stdout-junit-helper
0.4.1
## How to use
```java
public class Main {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name");
String name = scanner.nextLine();
System.out.println("Your name is " + name);
scanner.close();
}
}
class TestStdinStdout {
@Test
public void testAskName() {
StdinStdoutHelper helper = new StdinStdoutHelper()
.expectOutput("Enter your name")
.simulateInput("Pedro")
.expectOutput("Your name is Pedro");
helper.start();
main(null);
helper.stop();
}
}
```
### Contextual information on failure
In highly interactive programs, it can be difficult to understand what happened on certain
test failures. You can pass a `showDetailedErrors` and a `òutputBufferSize` to the
`StdinStdoutHelper` constructor, as in this example:
```java
StdinStdoutHelper helper = new StdinStdoutHelper(true, 20)
.expectOutput("Enter your name")
.simulateInput("Pedro")
.expectOutput("Enter your age")
.simulateInput("45")
.expectOutput("Pedro, you're 45 years old!");
```
This will show the last 20 (or less) lines of the interaction before the failure.
### Ignoring output lines
If the program writes several lines to the stdout that you don't want to test, you can use the function `expectNNumberOfLines`:
```java
StdinStdoutHelper helper = new StdinStdoutHelper()
.expectOutput("Enter the file name")
.simulateInput("data.txt")
.expectOutput("The data contained in the file:")
.expectNNumberOfLines(57) // suppose the file has 57 lines
.expectOutput("Read another file? [Y|N]")
.simulateInput("N");
```
### Dynamic behaviour
Suppose you have a program that prints a random number to the stdout, and you want to test that the printed number is
within a certain range. You can use the `matchOutput` which receives a predicate function. This function receives the
line that was printed to the screen and returns true if it matches the expected value. This is evaluated during the
program's execution.
#### Example in Kotlin
```kotlin
val helper = StdinStdoutHelper()
.expectOutput("Press enter to generate a random number between 1 and 100")
.simulateInput("") // press enter
.matchOutput { it in 1..100 }
```