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:
1
John Doe -- 4545 -- $15.67 per hour
Copied!
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.
1
John Doe -- 4545 -- $15.67 per hour
2
Nice you got a $1.0 raise!
3
John Doe -- 4545 -- $16.67 per hour
Copied!
​
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:

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
​
8
public class CodingRoomsUnitTests {
9
10
private final PrintStream standardOut = System.out;
11
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
12
​
13
@Before
14
public void setUp() {
15
System.setOut(new PrintStream(outputStreamCaptor));
16
}
17
​
18
@After
19
public void tearDown() {
20
System.setOut(standardOut);
21
}
22
​
23
@Test
24
public void testToString1() {
25
Employee e = new Employee("John Doe", 4545, 15.67);
26
System.out.println(e);
27
String answer = "John Doe -- 4545 -- $15.67 per hour".toLowerCase().trim().replace(" ", "");
28
String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
29
assertEquals(answer, output);
30
}
31
​
32
public void testToString2() {
33
Employee e = new Employee("Bob Bobbington", 5555, 10.5);
34
System.out.println(e);
35
String answer = "Bob Bobbington -- 5555 -- $10.5 per hour".toLowerCase().trim().replace(" ", "");
36
String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
37
assertEquals(answer, output);
38
}
39
​
40
public void testGetRaise1() {
41
Employee e = new Employee("Bob Bobbington", 5555, 10.5);
42
e.getRaise(1);
43
assertEquals(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", ""), "Nice you got a $1.0 raise!".toLowerCase().trim().replace(" ", ""));
44
System.setOut(standardOut);
45
System.setOut(new PrintStream(outputStreamCaptor));
46
System.out.println(e);
47
String answer = "Bob Bobbington -- 5555 -- $11.5 per hour".toLowerCase().trim().replace(" ", "");
48
String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
49
assertEquals(answer, output);
50
}
51
​
52
public void testGetRaise2() {
53
Employee e = new Employee("Bob Bobbington", 5555, 10.5);
54
e.getRaise(0.4);
55
assertEquals(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", ""), "Nice you got a $0.4 raise!".toLowerCase().trim().replace(" ", "");
56
System.setOut(standardOut);
57
System.setOut(new PrintStream(outputStreamCaptor));
58
System.out.println(e);
59
String answer = "Bob Bobbington -- 5555 -- $10.9 per hour".toLowerCase().trim().replace(" ", "");
60
String output = outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "");
61
assertEquals(answer, output);
62
}
63
}
Copied!

Sample Students Solution:

1
public class Employee
2
{
3
private String name;
4
private int id;
5
private double salary;
6
7
public Employee(String n, int i, double s)
8
{
9
name = n;
10
id = i;
11
salary = s;
12
}
13
public String toString()
14
{
15
return name + " -- " + id + " -- quot; + salary + " per hour";
16
}
17
​
18
public void getRaise(double amount)
19
{
20
salary += amount;
21
System.out.println("Nice you got a quot; + amount + " raise!");
22
}
23
}
Copied!
Last modified 8mo ago
Export as PDF
Copy link