Junit Fundamentals
Junit Fundamentals
Jan31,2015
Test suites
2
@Test
void testMax() {
assertEquals(7, max(3, 7));
assertEquals(3, max(3, -7));
}
}
void testMax() {
int x = max(3, 7);
if (x != 7) {
System.out.println("max(3, 7) gives " + x);
}
x = max(3, -7);
if (x != 3) {
System.out.println("max(3, -7) gives " + x);
}
}
public static void main(String[] args) {
new MyClass().testMax();
}
XP approach to testing
4
Consequences
Fewer bugs
More maintainable code
Continuous integrationDuring development, the program
always worksit may not do everything required, but what it
does, it does right
JUnit
5
Terminology
6
test suite
test runner
test fixture
import org.junit.*;
import static org.junit.Assert.*; // note static import
@Before
public void setUp() {
program = new MyProgram();
someVariable = 1000;
}
@After
public void tearDown() {
}
A simple example
10
Suppose you have a class Arithmetic with methods int multiply(int x, int y),
and boolean isPositive(int x)
import org.junit.*;
import static org.junit.Assert.*;
@Test
public void testMultiply() {
assertEquals(4, Arithmetic.multiply(2, 2));
assertEquals(-15, Arithmetic.multiply(3, -5));
}
@Test
public void testIsPositive() {
assertTrue(Arithmetic.isPositive(5));
assertFalse(Arithmetic.isPositive(-5));
assertFalse(Arithmetic.isPositive(0));
}
}
Assert methods I
11
Within a test,
Call the method being tested and get the actual result
Assert what the correct result should be with one of the assert
methods
These steps can be repeated as many times as necessary
@Before
void setUp() {
counter1 = new Counter(); // initialize the Counter here
}
@Test
public void testIncrement() {
assertTrue(counter1.increment() == 1);
assertTrue(counter1.increment() == 2);
}
@Test
public void testDecrement() {
assertTrue(counter1.decrement() == -1);
}
Notethateachtest
beginswithabrand
newcounter
Thismeansyoudont
havetoworryabout
theorderinwhichthe
testsarerun
Warning: equals
15
This method works great for Strings and a few other Java classes
For objects of classes that you create, you have to define equals
Assert methods II
16
assertEquals(expected, actual)
assertEquals(String message, expected, actual)
expected and actual must be both objects or the same
primitive type
For objects, uses your equals method, if you have defined it
properly, as described on the previous slide
assertSame(Objectexpected, Objectactual)
assertSame(String message, Objectexpected, Objectactual)
Asserts that two arguments refer to the same object
assertNotSame(Objectexpected, Objectactual)
assertNotSame(String message, Objectexpected, Objectactual)
Asserts that two objects do not refer to the same object
assertNull(Objectobject)
assertNull(String message, Objectobject)
Asserts that the object is null (undefined)
assertNotNull(Objectobject)
assertNotNull(String message, Objectobject)
Asserts that the object is null
fail()
fail(String message)
Causes the test to fail and throw an AssertionFailedError
Useful as a result of a complex test, when the other
assert methods arent quite what you want
@BeforeClass
public static void setUpClass() throws Exception {
// one-time initialization code
}
If you wish, you can declare one method to be executed just once,
when the class is first loaded
If you wish, you can declare one method to be executed just once,
to do cleanup after all the tests have been completed
@AfterClass
public static void tearDownClass() throws Exception {
// one-time cleanup code
}
@Test (timeout=10)
public void greatBig() {
assertTrue(program.ackerman(5, 5) > 10e12);
}
@Test (expected=IllegalArgumentException.class)
public void factorial() {
program.factorial(-5);
}
20
Test-Driven Development
(TDD)
It is difficult to add JUnit tests to an existing program
When tests are written first, you have a clearer idea what to
do when you write the methods
Because the tests are written first, the methods are
necessarily written to be testable
Writing tests first encourages you to write simpler, singlepurpose methods
Because the methods will be called from more than one
environment (the real one, plus your test class), they tend
to be more independent of the environment
Stubs
21
This helps test the teststo help make sure that an incorrect
method doesnt pass the tests
Ignoring a test
22
Test suites
23
@RunWith(value=Suite.class)
@SuiteClasses(value={
MyProgramTest.class,
AnotherTest.class,
YetAnotherTest.class
})
public class AllTests { }
JUnit in Eclipse
24
If you write your method stubs first (as on the previous slide),
Eclipse will generate test method stubs for you
To add JUnit 4 to your project:
Click Finish
Bar is green if
all tests pass,
red otherwise
Depending on your
preferences, this
window might show
only failed tests
Ran 10 of
the 10 tests
No tests
failed, but...
Something unexpected
happened in two tests
This is how
long the
test took
Recommended approach
26
The End
27
Ifyoudontunittestthenyouarentasoftwareengineer,youarea
typistwhounderstandsaprogramminglanguage.
MosesJones
1.Neverunderestimatethepowerofonelittletest.
2.Thereisnosuchthingasadumbtest.
3.Yourtestscanoftenfindproblemswhereyourenotexpecting
them.
4.Testthateverythingyousayhappensactuallydoeshappen.
5.Ifitsworthdocumenting,itsworthtesting.
AndyLester