# Input / Output Tests

This is an example of how you can mock input values and test the output stream of student code.&#x20;

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.&#x20;

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.&#x20;

*Note:  This code converts the output to all lowercase and removes spaces to compare against the correct value.*

#### **Grading Tests:**

```python
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:

{% code title="Example.java" %}

```python
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 + "!");
    }
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://auto-grade.joemazzone.net/java/input-output-tests.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
