Output Tests
This is an example of how you can test the output stream of student code.
This test makes sure the student's code prints "Hello World". It assumes the student's file name is Example.java. Replace the Example.main(null) call with whatever you 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:

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 testDefaultCase() {
25
Example.main(null);
26
assertEquals("Hello World".toLowerCase().trim().replace(" ", ""), outputStreamCaptor.toString().toLowerCase().trim().replace(" ", ""));
27
}
28
}
Copied!

Sample Solution:

Example.java
1
public class Example
2
{
3
public static void main(String[] args)
4
{
5
System.out.println("Hello World");
6
}
7
}
Copied!
Last modified 8mo ago
Export as PDF
Copy link