Skip to content
Anthony Christe edited this page Oct 2, 2013 · 5 revisions

JUnit

  • JUnit is the defacto testing framework for Java applications
  • Provides unit testing (and to some extent integration testing)
  • Comes standard with Eclipse/Netbeans
  • Heavily used in industry

Obtaining JUnit

Using JUnit

Creating test cases

  • Each test method should be a void method annotated with the @Test annotation
  • Each test should be specific and include only a few number of assertions (a single assertion is best, but not always possible)
  • @Test(expected = ExceptionName.class) can test is an exception is expected to occur
  • It's posibble to create a setup method that is called before each test using the @Before annotation
  • JUnit tests should be documented

Using JUnit from inside of jGrasp

Using JUnit from the command line (assuming no packages)

  • It's much easier to use JUnit from an IDE
  • Store the junit and hamcrest jar files in a known location on your computer (e.g. /home/anthony/dev/211/lib)
  • Copy your test class to the same directory as your source
  • Navigate your terminal window to the source root of your project
  • Compile your test case (should compile your entire project)
  • Compile anything that wasn't compiled from test case
  • Run the JUnit test
  • Note: that : are used when separating class paths in Linux/OS X, but I believe ; are used in Windows.

Compile the source files

javac -classpath [class paths] [source file(s)]

javac -classpath .:/home/anthony/dev/211/lib/junit-4.11.jar:/home/anthony/dev/211/lib/hamcrest-core-1.3.jar /home/anthony/dev/211/src/a02/Assignment2Tests.java

Running the tests

java -classpath [classpaths] org.junit.runner.JUnitCore [Test Class]

java -classpath .:/home/anthony/dev/211/lib/junit-4.11.jar:/home/anthony/dev/211/lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore Assignment02Tests

Clone this wiki locally