Presentation is loading. Please wait.

Presentation is loading. Please wait.

EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.

Similar presentations


Presentation on theme: "EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group."— Presentation transcript:

1 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Java Unit Mark Mosher mmosher@emrt.com Rochester Java Users Group

2 2 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Agenda n Unit Testing Principles n Benefits of Unit Testing n Java Unit History and Philosophy n The JUnit Framework n Testing Your Classes n Deployment Guidelines n Summary n Q & A

3 3 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Unit Testing Principles n Who? Developers, testing their own code n When? During implementation. n What? White box testing of “modules” - classes in our case. n How? Test normal and boundary inputs against expected results. n Why? A great way to test an API.

4 4 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Quantitative Benefits n Objective: Pass/Fail. n Repeatable: Retest after a change to verify no unintended side effects. n Measurable: Can count the number of tests that pass/fail. n Bounded: Narrow focus simplifies finding and fixing defects. n Cheaper: Find and fix defects early

5 5 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Qualitative Benefits n Assessment-oriented: Writing the unit test forces us to deal with design issues - n Assessment-oriented: Writing the unit test forces us to deal with design issues - cohesion, coupling. n n Confidence-building: Know what works. Also easier to change when it’s easy to retest.

6 6 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Java Unit History n Roots in eXtreme Programming n Kent Beck: Smalltalk Test Framework n Erich Gamma: Design Patterns n Wrote JUnit on plane to OOPSLA ‘97 n Description appeared in July 1998 Java Report. n Available on XP web site. n Other frameworks (C++, Python, VB…)

7 7 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Assumptions & Goals n Assumption: If a feature lacks an automated test, it doesn’t work. n Goal #1: Make it easy for developers to write tests. n Goal #2: Make it easy to create tests that retain their value over time. n Goal #3: Make it easy to leverage existing tests to create new ones.

8 8 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 The Java Unit Framework n Test n TestCase n TestResult n TestSuite n Assertions n TestRunner

9 9 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 junit.framework.Test n Public interface n Defines one method: run(testResult) n Component in Composite Pattern n Implemented by TestCase (Leaf in Composite pattern) n Implemented by TestSuite (applies Composite, maintains a collection of tests)

10 10 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 junit.framework.TestCase n Applies the Command pattern - create an object and give a method “execute” - run. n Implements the run() Template method. n Defines abstract setUp, runTest and tearDown methods. n This is the main class to extend for your unit tests.

11 11 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 junit.framework.TestResult n Applies the Collecting Parameter pattern - collects the results of running tests. n Counts number of tests run. n Counts failures - thrown AssertionFailedExceptions. n Counts errors - All other Throwables.

12 12 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 junit.framework.TestSuite n Defines addTest(Test) method. n Along with TestCase, implements the Test interface (run method). Allows suites of test suites. n Adds convenience constructor public static Test suite() uses reflection to find the testing methods.

13 13 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Assertions n Provided by TestCase. n Define pass/fail criteria - a must! n assert(boolean) n assert(String,boolean) n assertEquals(Object,Object) n assertEquals(String,Object,Object) n fail() n fail(String)

14 14 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 junit.*ui.TestRunner n Three versions –java.textui.TestRunner –java.ui.TestRunner –java.swingui.TestRunner n GUI (ui or swingui) good for developing single tests. n Text (textui) faster for suites.

15 15 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Testing Your Classes n 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.

16 16 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Testing Your Classes, 2 n Create one or more zero parameter methods that start with “test”, e.g. testInteger() and/or testParse(). n Junit will use reflection to execute these automatically for you. n Example IntegerTest...

17 17 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Deployment Guidelines n Have senior developer create and debug the first unit test. n Keep all unit tests in source control. n Parallel test package branch, e.g. test.lang.IntegerTest n Create AllTests suite for each package, then combine suites. n Example test suite...

18 18 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Summary n Developer’s test tool - 100% Java n Builds confidence in code base n Eases refactoring risks & worry n Quantitative results n Qualitative benefits n Free!

19 19 EMBEDDED REAL-TIME, INC. http://www.emrt.com December 8, 2015 Where to find n Version 3.2 available at www.xprogramming.com/software.htm n Source code n Jar file n Documentation & tutorials n Javadoc


Download ppt "EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group."

Similar presentations


Ads by Google