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:
importorg.junit.Before;importorg.junit.After;importorg.junit.Test;importstaticorg.junit.Assert.*;importjava.io.*;publicclassCodingRoomsUnitTests {privatefinalPrintStream standardOut =System.out;privatefinalByteArrayOutputStream outputStreamCaptor =newByteArrayOutputStream(); @BeforepublicvoidsetUp() {System.setOut(newPrintStream(outputStreamCaptor)); } @AfterpublicvoidtearDown() {System.setOut(standardOut); } @TestpublicvoidtestToString1() {Employee e =newEmployee("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); }publicvoidtestToString2() {Employee e =newEmployee("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); }publicvoidtestGetRaise1() {Employee e =newEmployee("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(newPrintStream(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); }publicvoidtestGetRaise2() {Employee e =newEmployee("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:
publicclassEmployee{ privateString name; privateint id; privatedouble salary;publicEmployee(String n,int i,double s) { name = n; id = i; salary = s; }publicStringtoString() {return name +" -- "+ id +" -- $"+ salary +" per hour"; } publicvoidgetRaise(double amount) { salary += amount;System.out.println("Nice you got a $"+ amount +" raise!"); }}