JUnit4 Survival Guide
by Michael Thomas
JUnit & TDD Home Page
This first section has the must know items to survive using JUnit.
Must Know ASAP
- Assert Test Methods
- assertEquals(expected, actual) - uses the equals() method for objects.
Primitives uses the value as expected.
also: assertEquals(description, expected, actual).
- assertEquals(double expected, double actual, double delta) - expected
values are considered equal if within the delta value.
ex:
- assertSame(expected, actual) - for objects only.
- assertTrue(actual) - fails if actual is false.
- assertFalse(actual) - fails if actual is true.
- assertNull(actual) - fails if actual is not null.
- assertNotNull(actual) - fails if actual is not null.
- Must haves for JUnit4:
- Imports - 2 imports
import
static
org.junit.Assert.*;
import
org.junit.Test;
- Class Name - always follows the naming convention: <ClassName>Test
ie: MyClassTest
- Java Notation - using a Java notation of @Test before all test methods.
- Test Methods - all methods that you want JUnit to run will follow these
rules:
- Visibility is: public
- Return type is: void
- Arguments: None!
- Method names start with lower case "test"
- Note: The order of running the methods can NOT be predicted! Do
not make test cases that rely on other methods running first!