edu/kit/informatik

ExampleTest.java

Download file

Diese Klasse illustriert, wie man mit unserer Terminal-Klasse ein Programm testen kann. Getestet wird die kleine Beispielklasse CampusManagement.

package edu.kit.informatik;

import static org.hamcrest.Matchers.*; //see: http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html

import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Example to demonstrate the use of the modified Terminal class.
 * 
 * @author Moritz Halm
 */
public class ExampleTest {

	private static final String[] NO_ARGS = {};

	@BeforeClass
	public static void enableTerminalTestingMode() {
		Terminal.enableTestingMode();
	}

	@After
	public void cleanUp() {
		// Important to tell the Terminal to check the last output
		Terminal.flush();
		Terminal.reset();
	}

	@Test
	public void test1() {
		// add student should print out OK
		Terminal.addSingleLineOutputThatIsExactly("add student", "OK");
		// list students can output students in any order
		Terminal.addMultipleLineOutputThatMatches("list students", containsInAnyOrder("Emil", "Paul"));
		// quit should not print out anything
		Terminal.addNoOutput("quit");

		// starts program with no arguments
		CampusManagement.main(NO_ARGS);

	}

	@Test
	public void test2() {
		// command should print out a single line that has a 2 in it somewhere
		Terminal.addSingleLineOutputThatMatches("get nrofstudents", containsString("2"));
		// the output for this command should start with "Error", since "bla bla
		// bla" isn't a valid command
		Terminal.addSingleLineOutputThatMatches("bla bla bla", startsWith("Error,"));
		// quit should not print out anything
		Terminal.addNoOutput("quit");

		CampusManagement.main(NO_ARGS);
	}
}