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:

What is your first name? 
Jane
What is your last name? 
Doe
Hello, Jane Doe!

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:

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.*;

public class CodingRoomsUnitTests {
    
    private final PrintStream standardOut = System.out;
    private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

    @Before
    public void setUp() {
        System.setOut(new PrintStream(outputStreamCaptor));
    }

    @After
    public void tearDown() {
        System.setOut(standardOut);
    }

    @Test
    public void testCase1() {
        String input = "Joe\nMazzone";
        InputStream in = new ByteArrayInputStream(input.getBytes());
        System.setIn(in);
        Example.main(null);
        assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Joe Mazzone!".toLowerCase().trim().replace(" ", "")));
    }

    @Test
    public void testCase2() {
        String input = "Clark\nKent";
        InputStream in = new ByteArrayInputStream(input.getBytes());
        System.setIn(in);
        Example.main(null);
        assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Clark Kent!".toLowerCase().trim().replace(" ", "")));
    }

    @Test
    public void testCase3() {
        String input = "Bruce\nWayne";
        InputStream in = new ByteArrayInputStream(input.getBytes());
        System.setIn(in);
        Example.main(null);
        assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Bruce Wayne!".toLowerCase().trim().replace(" ", "")));
    }
}

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
import java.util.Scanner;

public class Example
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("What is your first name?");
        String fName = scan.nextLine();
        
        System.out.println("What is your last name?");
        String lName = scan.nextLine();
        
        System.out.println("Hello, " + fName + " " + lName + "!");
    }
}

Last updated