-
Notifications
You must be signed in to change notification settings - Fork 4
JUnit Testing
Anthony Christe edited this page Oct 2, 2013
·
5 revisions
- 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
- Recent versions of Eclipse and Netbeans should include JUnit by default
- Otherwise, it can be downloaded at http://junit.org/
- You will need to download the junit jar and the hamcrest jar as described at https://github.com/junit-team/junit/wiki/Download-and-Install
- Getting Started Guide
- JUnit Wiki
- In Eclipse, right click on source directory and select "Add New JUnit Test case" (should be similar in Netbeans)
- Assertions
- Assertions API
- 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
- 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