Professional Documents
Culture Documents
Junit 4: The Ultimate Unit Test Framework
Junit 4: The Ultimate Unit Test Framework
• Introduction
• Test Cases and Test Suites
• Parameterized Tests
• Running JUnit Tests
Introduction to JUnit
3
copyright 2008 trainologic LTD
JUnit 4
Introduction to JUnit
4
copyright 2008 trainologic LTD
JUnit 4
Introduction to JUnit
5
copyright 2008 trainologic LTD
JUnit 4
Introduction to JUnit
6
copyright 2008 trainologic LTD
JUnit 4
Installing JUnit
• Download junit.zip.
• Unzip it.
• Add the junit.jar to you classpath.
• And.. Voila... you are good to go.
7
copyright 2008 trainologic LTD
JUnit 4
• Introduction
• Test Cases and Test Suites
• Parameterized Tests
• Running JUnit Tests
Test
@Test
public void testAdd() throws Exception {
double first = 5;
double second = 5;
Calculator calc = new Calculator();
Assert.assertEquals(first + second, calc.add(first, second));
}
9
copyright 2008 trainologic LTD
JUnit 4
Test
10
copyright 2008 trainologic LTD
JUnit 4
Test
@Test(expected=LoginException.class)
public void testLoginException() throws LoginException {
...
}
11
copyright 2008 trainologic LTD
JUnit 4
Test
12
copyright 2008 trainologic LTD
JUnit 4
Test
• There is a wide variety of assertions:
13
copyright 2008 trainologic LTD
JUnit 4
Test
• Another way to fail the test is by calling the method
fail().
@Test
public void testSomething() throws Exception {
...
switch(i) {
case a:
... break;
case b:
... break;
default:
Assert.fail();
}
}
14
copyright 2008 trainologic LTD
JUnit 4
Test
• It is also possible to add a timeout to the test.
• In case the test hasn’t finished befoer timeout, it will
fail.
• Set the timeout attribute of the @Test annotation to
the allowed time in milliseconds.
@Test(timeout=1)
public void testSomething() throws Exception {
...
}
15
copyright 2008 trainologic LTD
JUnit 4
Test Cases
@Test
public void testAdd() throws Exception {...}
@Test
public void testSubstruct() throws Exception {...}
16
copyright 2008 trainologic LTD
JUnit 4
Test Cases
• Sometimes, tests share resources.
Will the following example work?
17
copyright 2008 trainologic LTD
JUnit 4
Test Cases
• Even though ShoppingCart is a shared resource, we
wouldn’t want both tests to use the same cart.
18
copyright 2008 trainologic LTD
JUnit 4
Fixtures
• Fixtures are simple methods annotated with @Before
and @After.
19
copyright 2008 trainologic LTD
JUnit 4
Fixtures
init() - @Before
20
copyright 2008 trainologic LTD
JUnit 4
Fixtures
20
copyright 2008 trainologic LTD
JUnit 4
Fixtures
20
copyright 2008 trainologic LTD
JUnit 4
Fixtures
init() - @Before
20
copyright 2008 trainologic LTD
JUnit 4
Fixtures
20
copyright 2008 trainologic LTD
JUnit 4
Fixtures
20
copyright 2008 trainologic LTD
JUnit 4
Fixtures
20
copyright 2008 trainologic LTD
JUnit 4
Shared Fixtures
• Shared Fixtures run once for all the Test Case, thus
allowing us to really share the object between tests.
21
copyright 2008 trainologic LTD
JUnit 4
Protected Methods
• Testing public methods is simple, but how can we test
protected methods?
• Easy... just put the test code in the same package (not
necessarily in the same source folder).
22
copyright 2008 trainologic LTD
JUnit 4
Private Methods
• OK... but what about private methods.
• Well, if you need to unit test a private method, it is
usually a sign the method should be promoted to
another class for reuse.
23
copyright 2008 trainologic LTD
JUnit 4
Test Suites
• Test Suites are a bundle of Test Cases wrapped
together.
24
copyright 2008 trainologic LTD
JUnit 4
Test Suites
@RunWith(Suite.class)
@SuiteClasses(value={FirstTest.class, SecondItemTest.class})
public class AllTests {}
25
copyright 2008 trainologic LTD
JUnit 4
• Introduction
• Test Cases and Test Suites
• Parameterized Tests
• Running JUnit Tests
Parameterized Tests
• Sometimes, we want to run the same test code with
different parameters, several times.
27
copyright 2008 trainologic LTD
JUnit 4
Parameterized Tests
28
copyright 2008 trainologic LTD
JUnit 4
Parameterized Tests
@RunWith(Parameterized.class)
public class ParameterizedExample {
double first, second; Calculator calc;
29
copyright 2008 trainologic LTD
JUnit 4
Parameterized Tests
30
copyright 2008 trainologic LTD
JUnit 4
• Introduction
• Test Cases and Test Suites
• Parameterized Tests
• Running JUnit Tests
Running Tests
32
copyright 2008 trainologic LTD
JUnit 4
Command-Line
33
copyright 2008 trainologic LTD
JUnit 4
Using IDE
34
copyright 2008 trainologic LTD
JUnit 4
35
copyright 2008 trainologic LTD
JUnit 4
36
copyright 2008 trainologic LTD
JUnit 4
37
copyright 2008 trainologic LTD
JUnit 4
<junit>
<test name="my.test.TestCase"/>
</junit>
38
copyright 2008 trainologic LTD
JUnit 4
<formatter type="plain"/>
39
copyright 2008 trainologic LTD
JUnit 4
Summary
40
copyright 2008 trainologic LTD