Input / Output Tests
This is an example of how you can mock input values and test the output stream of student code.
The student must use the Scanner's nextLine() function to prompt the user for their first name, the again for their last name. Then, print Hello, firstname lastname! including the person's first and last name.
Sample Run:
1
What is your first name?
2
Jane
3
What is your last name?
4
Doe
5
Hello, Jane Doe!
Copied!
It assumes the student's file name is Example.java. This can be easily replaced with whatever your student's file name is.
Note: This code converts the output to all lowercase and removes spaces to compare against the correct value.

Grading Tests:

1
import org.junit.Before;
2
import org.junit.After;
3
import org.junit.Test;
4
import static org.junit.Assert.*;
5
import java.io.*;
6
​
7
public class CodingRoomsUnitTests {
8
9
private final PrintStream standardOut = System.out;
10
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
11
​
12
@Before
13
public void setUp() {
14
System.setOut(new PrintStream(outputStreamCaptor));
15
}
16
​
17
@After
18
public void tearDown() {
19
System.setOut(standardOut);
20
}
21
​
22
@Test
23
public void testCase1() {
24
String input = "Joe\nMazzone";
25
InputStream in = new ByteArrayInputStream(input.getBytes());
26
System.setIn(in);
27
Example.main(null);
28
assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Joe Mazzone!".toLowerCase().trim().replace(" ", "")));
29
}
30
​
31
@Test
32
public void testCase2() {
33
String input = "Clark\nKent";
34
InputStream in = new ByteArrayInputStream(input.getBytes());
35
System.setIn(in);
36
Example.main(null);
37
assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Clark Kent!".toLowerCase().trim().replace(" ", "")));
38
}
39
​
40
@Test
41
public void testCase3() {
42
String input = "Bruce\nWayne";
43
InputStream in = new ByteArrayInputStream(input.getBytes());
44
System.setIn(in);
45
Example.main(null);
46
assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Bruce Wayne!".toLowerCase().trim().replace(" ", "")));
47
}
48
}
Copied!
The input String replaces the programs input stream. Each input must be separated by a newline character - \n.
You will also notice that the assertion doesn't compare the output directly with what is expected. Instead it sees if the output stream .endsWith() the expected output. This is because the we print other values as prompts for the Scanner input, so we only want to check the last thing printed. If you want to include the prompts to ensure students properly included them, you can use assertEqual() and and compare the entire output stream.

Sample Solution:

Example.java
1
import java.util.Scanner;
2
​
3
public class Example
4
{
5
public static void main(String[] args)
6
{
7
Scanner scan = new Scanner(System.in);
8
9
System.out.println("What is your first name?");
10
String fName = scan.nextLine();
11
12
System.out.println("What is your last name?");
13
String lName = scan.nextLine();
14
15
System.out.println("Hello, " + fName + " " + lName + "!");
16
}
17
}
Copied!
Last modified 15d ago
Export as PDF
Copy link