The document provides an introduction to Java unit testing and code coverage. It discusses test-driven development and the JUnit testing framework, including annotations, test results, and Ant integration. It also covers using mock objects with Mockito, performing code coverage analysis, and code coverage tools like Emma and Cobertura.
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Java Unit Test and Coverage Introduction
1. Java Unit Test and Coverage Introduction
Alex Su
2010/07/21
Copyright 2010 TCloud Computing Inc.
2. Agenda
•Test-driven development
•Junit
•Ant Integration
•Mock Object
• Mockito
•Code Coverage Analysis
•Coverage Tool
• Emma
• Cobertura
•Case Study
Trend Micro Confidential
3. Test-driven development
Test-driven development (TDD) is a software development
technique that relies on the repetition of a very short
development cycle
•Add a test
•Run all tests and see if the new one fails
•Write some code
•Run the automated tests and see them succeed
•Refactor code
Trend Micro Confidential
4. Test-driven development - Scenario
Given/When/Then
Example 1: New lists are empty
Given a new list
Then the list should be empty.
Example 2: Lists with things in them are not empty.
Given a new list
When we add an object
Then the list should not be empty.
Trend Micro Confidential
5. Test-driven development - Scenario
class ListTest {
@Test
public void shouldKnowWhetherItIsEmpty() {
List<String> list1 = new ArrayList<String>();
assertTrue(list1.isEmpty());
List<String> list2 = new ArrayList<String>();
list2.add("element");
assertFalse(list2.isEmpty());
}
}
Trend Micro Confidential
7. JUnit
• JUnit is a unit testing framework for the Java programming
language.
• JUnit is linked as a JAR at compile-time; the framework
resides under packages junit.framework for JUnit 3.8 and
earlier and under org.junit for JUnit 4 and later.
Trend Micro Confidential
8. JUnit
• Test Runner
A Runner runs tests. You will need to subclass Runner when using
RunWith to invoke a custom runner.
• Test Fixture
A test fixture represents the preparation needed to perform one or more
tests
• Test Case
A test case defines the fixture to run multiple tests.
• Test Suite
A Test Suite is a Composite of Tests. It runs a collection of test cases.
Trend Micro Confidential
9. Junit – Test Result
• Success
• Failure
A failure is when one of your assertions fails, and your JUnit
test notices and reports the fact.
• Error
An error is when some other Exception occurs--one you
haven't tested for and didn't expect, such as a
NullPointerException or an
ArrayIndexOutOfBoundsException.
Trend Micro Confidential
15. Mock Object
•supplies non-deterministic results (e.g. the current
time or the current temperature);
•has states that are difficult to create or reproduce
(e.g. a network error);
•is slow (e.g. a complete database, which would
have to be initialized before the test);
•does not yet exist or may change behavior;
•would have to include information and methods
exclusively for testing purposes (and not for its
actual task).
Trend Micro Confidential
16. Mock Object - Mockito
•Argument matchers
•Verifying exact number of invocations / at least x /
never
•Verification in order
•Shorthand for mocks creation - @Mock annotation
•Stubbing consecutive calls
•Spying on real objects
•Aliases for behavior driven development
Trend Micro Confidential
17. Mock Object - Mockito
public class ListTest {
private List<String> mockedList;
@Before
public void initMocks() {
mockedList = mock(List.class);
given(mockedList.get(0)).willReturn("first", "second");
given(mockedList.get(1)).willThrow(new
IndexOutOfBoundsException());
}
@Test
public void getList() {
assertEquals("first", mockedList.get(0));
assertEquals("second", mockedList.get(0));
}
@Test(expected = IndexOutOfBoundsException.class)
public void getListWithException() {
mockedList.get(1);
}
}
Trend Micro Confidential
18. Code Coverage Analysis
•Finding areas of a program not exercised by a set
of test cases.
•Creating additional test cases to increase coverage.
•Determining a quantitative measure of code
coverage, which is an indirect measure of quality.
•Identifying redundant test cases that do not
increase coverage.
Trend Micro Confidential
21. Code Coverage Analysis
•Condition/Decision Coverage(C/DC)
•Modified Condition/Decision Coverage(MC/DC)
boolean a = true, b = true;
if(a && b) {
System.out.println("true");
} else {
System.out.println("false");
}
if(a || b) {
System.out.println("true");
} else {
System.out.println("false");
}
Trend Micro Confidential
22. Code Coverage Analysis
•Path Coverage
Y
A = 3 and X = 5 A>1
A = 0 and X = 3
A = 0 and X = 0 N X=0
A = 2 and X = 0
A=2 Y
or
X>1
N X = 20
print x
Trend Micro Confidential
25. Coverage Tool
Possible feature Runtime Bytecode Source code
Gathers method coverage yes yes yes
Gathers statement coverage line only yes yes
Gathers branch coverage indirectly yes yes
Can work without source yes yes no
Requires separate build no no yes
Requires specialised runtime yes not accurate no
View coverage data inline
not accurate yes yes
with source
Source level directives to
no no yes
control coverage gathering
Runtime performace high impact variable variable
Container friendly no not accurate yes
Trend Micro Confidential
26. Coverage Tool
•Reports are filterable so you can tell what needs to
be evaluated for code coverage.
•offline and on-the-fly instrumentation.
•Ant integration.
•JUnit integration.
Trend Micro Confidential
27. Coverage Tool - Emma
•Last update : 2005-06-13
•Stats on both class and method coverage
•Partial/fractional line coverages is unique trait -
shown in yellow when there are multiple
conditions in a conditional block
•Stricter code coverage.
•Integration with Eclipse available -
http://www.eclemma.org/
•Better documentation than cobertura.
•Instrumentation process is faster than cobertura.
•Standalone library and does not have any external
dependencies.
•Common public license 1.0 friendlier that GPL.
Trend Micro Confidential
28. Coverage Tool - Cobertura
• Last update : 2010-03-03
• GPL'd version of JCoverage (which is commercial).
• Prettier reports.
• Branch/block and line coverages only - no class or
method level coverage.
• How many times a line has been executed
• <cobertura-check> where one can specify percentage
of coverage that's a MUST or else build fails.
• Data merge feature - good for QA labs... for merging
coverage data to prepare historical trend graphs.
• Depends on other third party libraries.
• Integration with Eclipse available -
http://ecobertura.johoop.de/
Trend Micro Confidential