JUnit, - Rochester Java User`s Group
Download
Report
Transcript JUnit, - Rochester Java User`s Group
Java Unit
Mark Mosher
[email protected]
Rochester Java Users Group
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
Agenda
Unit Testing Principles
Benefits of Unit Testing
Java Unit History and Philosophy
The JUnit Framework
Testing Your Classes
Deployment Guidelines
Summary
Q & A
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Unit Testing Principles
Who? Developers, testing their own code
When? During implementation.
What? White box testing of “modules” classes in our case.
How? Test normal and boundary inputs
against expected results.
Why? A great way to test an API.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Quantitative Benefits
Objective: Pass/Fail.
Repeatable: Retest after a change to
verify no unintended side effects.
Measurable: Can count the number of
tests that pass/fail.
Bounded: Narrow focus simplifies finding
and fixing defects.
Cheaper: Find and fix defects early
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Qualitative Benefits
Assessment-oriented: Writing the unit test
forces us to deal with design issues cohesion, coupling.
Confidence-building: Know what works.
Also easier to change when it’s easy to
retest.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Java Unit History
Roots in eXtreme Programming
Kent Beck: Smalltalk Test Framework
Erich Gamma: Design Patterns
Wrote JUnit on plane to OOPSLA ‘97
Description appeared in July 1998 Java
Report.
Available on XP web site.
Other frameworks (C++, Python, VB…)
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Assumptions & Goals
Assumption: If a feature lacks an
automated test, it doesn’t work.
Goal #1: Make it easy for developers to
write tests.
Goal #2: Make it easy to create tests that
retain their value over time.
Goal #3: Make it easy to leverage existing
tests to create new ones.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
The Java Unit Framework
Test
TestCase
TestResult
TestSuite
Assertions
TestRunner
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
junit.framework.Test
Public interface
Defines one method: run(testResult)
Component in Composite Pattern
Implemented by TestCase (Leaf in
Composite pattern)
Implemented by TestSuite (applies
Composite, maintains a collection of tests)
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
junit.framework.TestCase
Applies the Command pattern - create an
object and give a method “execute” - run.
Implements the run() Template method.
Defines abstract setUp, runTest and
tearDown methods.
This is the main class to extend for your
unit tests.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
junit.framework.TestResult
Applies the Collecting Parameter pattern collects the results of running tests.
Counts number of tests run.
Counts failures - thrown
AssertionFailedExceptions.
Counts errors - All other Throwables.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
junit.framework.TestSuite
Defines addTest(Test) method.
Along with TestCase, implements the Test
interface (run method). Allows suites of
test suites.
Adds convenience constructor public static
Test suite() uses reflection to find the
testing methods.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Assertions
Provided by TestCase.
Define pass/fail criteria - a must!
assert(boolean)
assert(String,boolean)
assertEquals(Object,Object)
assertEquals(String,Object,Object)
fail()
fail(String)
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
junit.*ui.TestRunner
Three versions
– java.textui.TestRunner
– java.ui.TestRunner
– java.swingui.TestRunner
GUI (ui or swingui) good for developing
single tests.
Text (textui) faster for suites.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Testing Your Classes
What to test
– Utility or Home object. Important or hard to
test any other way.
– Create test class, e.g. TestInteger to test the
Integer class.
– Have the test class extend
junit.framework.TestCase.
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Testing Your Classes, 2
Create one or more zero parameter
methods that start with “test”, e.g.
testInteger() and/or testParse().
Junit will use reflection to execute these
automatically for you.
Example IntegerTest...
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Deployment Guidelines
Have senior developer create and debug
the first unit test.
Keep all unit tests in source control.
Parallel test package branch, e.g.
test.lang.IntegerTest
Create AllTests suite for each package,
then combine suites.
Example test suite...
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Summary
Developer’s test tool - 100% Java
Builds confidence in code base
Eases refactoring risks & worry
Quantitative results
Qualitative benefits
Free!
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›
Where to find
Version 3.2 available at
www.xprogramming.com/software.htm
Source code
Jar file
Documentation & tutorials
Javadoc
April 12, 2016
EMBEDDED REAL-TIME, INC.
http://www.emrt.com
‹#›