User Tools

Site Tools



  • Project 3 neither reads from standard input nor writes gradeable text to standard output, so the mechanism below to redirect these two streams to files is not necessary for this project. Instead, your JUnit test routines can just call main directly with different versions of the String[] args array to indicate different input test files.
  • For command-line testing in the UNIX/Linux environment, see the new section below.

Testing Your Project

Test early, test often.

Use a JUnit test case. JUnit is a Java package that facilitates “unit testing”: creation, execution, and evaluation of repeatable tests on your code. It is designed to support incremental testing (test-driven development), but can also be used in a variety of ways including “whole program” testing.

Both DrJava and Eclipse include JUnit support. (Chapter 7 in the DrJava help documentation has details.)

Testing Programs that Read from Standard In and Write to Standard Out

For projects that take input from the command line (or redirected from a file) and produce gradable results to standard out, automated testing requires that these streams be redirected in the JUnit test class. This section explains how to do that.

To use JUnit to test your main method with different input streams, you need to make a minor structural change. The idea is this: Use the main method to gather parameters and setup input and output streams, then call a second main method, mainWork(…), to do the actual work of your program. This arrangement allows you to programmatically set up for and call mainWork(…) from your JUnit test class.

Your MainClass methods looks like this…

  public static void main(String[] args)
      mainWork(new Scanner(, System.out);
  static void mainWork(Scanner stdin, PrintStream stdout)
      ... original main method, using stdin for input and stdout for output...

Your MainClassTester class includes a number of methods. By default, JUnit runs all those starting with test each time you do a test run. Here is a sample test method that sets up specific files for reading and writing, then calls the mainWork(…) method.

  public final void testFile()
      String prefix = "input1000";
      String fileIn = prefix + ".txt";
      String fileOut = prefix + ".out";
      try {
          Scanner stdin = new Scanner(new File(fileIn));
          PrintStream stdout = new PrintStream(new FileOutputStream(fileOut));
          MainClass.mainWork(stdin, stdout);
      } catch (FileNotFoundException e) {
          fail("file '" + fileIn + "' not found");

In DrJava, using the “Test Project” button (bound to Ctrl-T) runs all your unit tests. The “Run” button (bound to F2) continues to run your main method (prompting you for manual input in the Interactions window).

Of course, you can generalize the testFile() method to testFiles() and call a helper method that allows testing of several input files. Also, you can open and read the generated output to see if it is correct by comparing it to the contents of a known correct file.

For Project 2B, you'll need to parse args[0] to get the number of threads and pass that as another parameter to mainWork(…).


You'll need imports to get access to a number of these symbols. The input/output ones are in*, or you can import them explicitly. Here's a set that works in the test class…

  import junit.framework.TestCase;
  import java.util.Scanner;

Command-Line JUnit Testing in UNIX

Here is a minimalist way to do command-line JUnit testing in the UNIX/Linux environment.

  1. Download the latest JUnit jar file from (as of this writing, junit-4.5.jar).
  2. Put this jar file into the same directory as your test class and other project files.
  3. Compile your test class, adding junit-4.5.jar to your classpath (either using the -cp option to javac or with the sample script below).
  4. Run your test class using the text-based JUnit runner, junit.textui.TestRunner.

Here is a script, named javat, that compiles and runs a test class given as the command line argument.

CLASSPATH=".:junit-4.5.jar"; export CLASSPATH
javac $
java junit.textui.TestRunner $1

If your test class is named, say, SpellCheckerTester, you would type…

$ ./tester SpellCheckerTester

For more general-purpose use, put the jar file in your ~/lib directory, add the correct CLASSPATH variable to your startup file (e.g., to ~/.bashrc) or edit it into the tester script, and move the tester script to your personal ~/bin directory (adding that directory to your PATH, if necessary).

junit-test-example.txt · Last modified: 2008/10/25 08:58 by jtkorb