Class - .toString() and Void Method Tests

This page shows how you can do some auto-grading of classes that only have void methods (methods that do not return a value) and a .toString() method.

This is completed by testing the output stream produced by the methods as they are called.

In this example, the comparison is completed with all character converted to lowercase and all spaces removed. If you want a pure character for character match, remove the .toLowerCase().trim().replace(" ", "") from the assertion.

Example 1 - Employee Class

Students had to create an Employee class that has the following instance variables:

  • String employee’s name

  • int their 4 digit id number

  • double hourly salary

Then, create a constructor method that sets each instance variable to the parameter values given to the constructor call.

Also, they must create a toString() method that prints the Employee's name, 4 digit id, and salary in the following format:

John Doe -- 4545 -- $15.67 per hour

Lastly, they must create a method called getRaise(double amount) that increases the employee's salary by the specified amount and prints “Nice you got a $” + amount + “ raise!”

For example, if we print the Employee object, call getRaise(1.0), and print the Employee object again, the following would be created.

John Doe -- 4545 -- $15.67 per hour
Nice you got a $1.0 raise!
John Doe -- 4545 -- $16.67 per hour

To auto-grade this, use the following -- you can run as many tests as your want!

First, there are two tests that ensure the .toString() method is working properly. They create an object with the Employee class, print the object, and ensure the print stream matches the expected output (remember, this example compares the two strings as all lowercase and with spaces removed).

Second, the .getRaise() method is tested with two different tests. The object is instantiated, .getRaise() is called, and then the object is printed to confirm that it effected the salary field. Note that that the print stream is reset between .getRaise() and printing the object so that each print statement can be tested asserted separately but within the same test.

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 testToString1() {
        Employee e = new Employee("John Doe", 4545, 15.67);
        System.out.println(e);
        String answer = "John Doe -- 4545 -- $15.67 per hour".toLowerCase().trim().replace(" ", "");
        String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
        assertEquals(answer, output);
    }

    public void testToString2() {
        Employee e = new Employee("Bob Bobbington", 5555, 10.5);
        System.out.println(e);
        String answer = "Bob Bobbington -- 5555 -- $10.5 per hour".toLowerCase().trim().replace(" ", "");
        String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
        assertEquals(answer, output);
    }

    public void testGetRaise1() {
        Employee e = new Employee("Bob Bobbington", 5555, 10.5);
        e.getRaise(1);
        assertEquals(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", ""), "Nice you got a $1.0 raise!".toLowerCase().trim().replace(" ", ""));
        System.setOut(standardOut);
        System.setOut(new PrintStream(outputStreamCaptor));
        System.out.println(e);
        String answer = "Bob Bobbington -- 5555 -- $11.5 per hour".toLowerCase().trim().replace(" ", "");
        String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
        assertEquals(answer, output);
    }

    public void testGetRaise2() {
        Employee e = new Employee("Bob Bobbington", 5555, 10.5);
        e.getRaise(0.4);
        assertEquals(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", ""), "Nice you got a $0.4 raise!".toLowerCase().trim().replace(" ", "");
        System.setOut(standardOut);
        System.setOut(new PrintStream(outputStreamCaptor));
        System.out.println(e);
        String answer = "Bob Bobbington -- 5555 -- $10.9 per hour".toLowerCase().trim().replace(" ", "");
        String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
        assertEquals(answer, output);
    }   
}

Sample Students Solution:

public class Employee 
{ 
    private String name; 
    private int id; 
    private double salary;
    
    public Employee(String n, int i, double s) 
    {
        name = n;
        id = i;
        salary = s;
    }
    public String toString() 
    {
        return name + " -- " + id + " -- $" + salary + " per hour";
    } 

    public void getRaise(double amount)
    {
        salary += amount;
        System.out.println("Nice you got a $" + amount + " raise!");
    }
}

Last updated