SlideShare ist ein Scribd-Unternehmen logo
1 von 74
Downloaden Sie, um offline zu lesen
Practical Unit Testing:
“Good programmers write code, great programmers write tests.”




    Java Student User Group
              2009

           Peter Kofler, ‘Code Cop’
               @codecopkofler
              www.code-cop.org
        Copyright Peter Kofler, licensed under CC-BY.
PETER KOFLER, CODE-COP.ORG                  FANATIC ABOUT CODE QUALITY




                             Peter Kofler
  • Ph.D. in Applied Math
  • Java developer since 1999

  • fanatic about code quality
  • appointed ‘Code Cop’ in 2006

  • Senior Software Engineer at s-IT Solutions
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                             Agenda
  • JUnit Basics
        – Test Methods, Assertions, Fixtures
  • Advanced Topics
        – Privates, Mocks, Timings, Singletons, J2EE
  • Tuning
  • Code Coverage
  • JUnit Extensions
        – Tools, Scripting, JUnit and the Build
PETER KOFLER, CODE-COP.ORG                        FANATIC ABOUT CODE QUALITY




                             A Little Survey...
  •    Who knows xUnit?
  •    Who knows JUnit 4?
  •    Who ever wrote a unit test?
  •    Who writes tests and tries to write them first?
  •    Who checks the coverage?
  •    Who ever produced a bug?
PETER KOFLER, CODE-COP.ORG                       FANATIC ABOUT CODE QUALITY




                         We Make Mistakes
  • at least I do... 
  • number of bugs proportional loc
        – 2 bugs per 1.000 loc (7 or even more...)
        – 1 bug per 10.000 loc in critical software
  • be paranoid when you write software
        – Assume you have lots of bugs.
        – Try to find these bugs aggressively.
I find your lack of tests disturbing.
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                     Wait - We Have Tests
  • almost every project has some “tests”
  • almost all of them are useless 
        – experiments how to use some library
        – main methods, waiting for user input, ...
        – tests that initialise the whole application and
          check nothing
        – tests that fail since long, etc.
                                              No You Don’t!
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                             JUnit
  • a unit testing framework
  • active, dynamic black-box tests
        – some call it white-box tests (tbd)
  • works best with a number of small tests
  • You should know it (no excuses!)
        – You should use it (no excuses!)
        – I will not explain it here  www.junit.org
“Keep the bar green to keep the code clean”
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




                             Test Methods
  • unit test tests the methods of a single class
  • test case tests the response of a single
    method to a particular set of inputs
        – multiple test cases for a single method
        – public void testMethod() or @Test
        – test methods should be short, simple
        – tests without test methods are pointless
           Findbugs and PMD
PETER KOFLER, CODE-COP.ORG                       FANATIC ABOUT CODE QUALITY




                             Assertions
  • Don’t do any output from your unit tests!
  • check expectations programmatically
        – assertEquals,assertNull,assertTrue,...
        – test method without assert is pointless (PMD)
        – one test method - one assertion (tbd)
               • some work around PMD with assertTrue(true)
                   PMD: UnnecessaryBooleanAssertion
  • test runner reports failure on each test
PETER KOFLER, CODE-COP.ORG                       FANATIC ABOUT CODE QUALITY




                             Proper Assertions
  • add messages to asserts (tbd) ( PMD)
  • assertTrue(a.equals(b)) no message,
    better use assertEquals(a,b) ( PMD)
  • assert in Thread.run() not noticed
    ( Findbugs: IJU_...)
  • assert float in ranges of precision:
       assertEquals(expected, actual,
                    5*Math.ulp(expected))
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




                        Assertions (JUnit 4)
  • assertArrayEquals(.) for atom arrays
    and Object
  • but assertEquals(int,int) removed
        –    not needed any more (auto boxing)
        –    problems with mixed params, e.g. (int,byte)
        –    JUnit 3: promoted to (int,int), succeeds
        –    JUnit 4: boxed to (Integer,Byte), fails
PETER KOFLER, CODE-COP.ORG               FANATIC ABOUT CODE QUALITY




                      Asserting Exceptions
  • JUnit 3 try-catch code:
  try {
    // code that should cause an exception
    fail("no exception occurred");
  } catch (SomeException success) {
    // check exception type/parameters


  • JUnit 4: @Test(expected)annotation:
  @Test(expected=SomeException.class)
  public void testThrows() {
    // code that should cause an exception
PETER KOFLER, CODE-COP.ORG                        FANATIC ABOUT CODE QUALITY




                             Fixtures (JUnit 3)
  • sets up data needed to run tests
  • JUnit 3: setUp(), tearDown()
        –    don’t forget to call super.setUp() first
        –    don’t forget to call super.tearDown() last
        –    don’t forget it (!)
        –    Findbugs: IJU_SETUP_NO_SUPER,IJU_...
  • for fixture in JUnit 3.x that runs only once,
    use the TestSetup decorator
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




     JUnit 3 TestSetup Decorator
  public class TheTest extends TestCase {
   // test methods ...

    public static Test suite() {
     return new TestSetup(new TestSuite(TheTest.class)) {
      protected void setUp() throws Exception {
        super.setUp();
        // set-up code called only once
      }
      protected void tearDown() throws Exception {
        // tear-down code called only once
        super.tearDown();
      }
     };
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                             Fixtures
  • JUnit 4: @Before, @After
        – run methods of super classes
        – only once: @BeforeClass, @AfterClass
  • test data in database is problematic
        – test has to insert its own preconditions
        – large data sets  DbUnit
  • Remember: Test data is more likely to be
    wrong than the tested code!
PETER KOFLER, CODE-COP.ORG                          FANATIC ABOUT CODE QUALITY




                  Test Code Organisation
  • test code loc ~ functional code loc
  • same quality as production code
        – always built with test code
        – execute tests as soon/often as possible
  • parallel package hierarchy
        – no *.test sub-packages! (tbd)
        – folder test (simple), src/test/java (Maven)
        – package-access!
PETER KOFLER, CODE-COP.ORG                FANATIC ABOUT CODE QUALITY




                  Test Class Organisation
  • create your own base test case(s)
        – named *TestCase or *TC (not *Test)
        – common methods, initialisation code
        – custom asserts, named assert* (PMD)


  • name test classes <tested class>Test
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                             Agenda
  • JUnit Basics 
        – Test Methods, Assertions, Fixtures
  • Advanced Topics
        – Privates, Mocks, Timings, Singletons, J2EE
  • Tuning
  • Code Coverage
  • JUnit Extensions
        – Tools, Scripting, JUnit and the Build
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




                        Testing Private Data
  • “Wishing for White Box Testing (i.e. check
    a private field) is not a testing problem, it is
    a design problem.”
        – If you want to check internals - improve design.


  • if you have to:
        – Reflection: member.setAccessible(true)
Mocks
PETER KOFLER, CODE-COP.ORG                         FANATIC ABOUT CODE QUALITY




                        When to Use Mocks
  • To have a “real” unit test (cut dependencies)
  • “It is much simpler to simulate behaviour than
    it is to recreate that behaviour.”
  • use a mock when the real object is
        –   non-deterministic (e.g. current time)
        –   problematic during execution (e.g. user input)
        –   difficult to trigger (e.g. network error)
        –   not existing yet (team collaboration)
PETER KOFLER, CODE-COP.ORG                FANATIC ABOUT CODE QUALITY




                  How to Mock an Object
  • by hand
     – implement its interface (Eclipse Ctrl-1)
     – subclass it (beware complex constructors)
  • java.lang.reflect.Proxy
     – since 1.3
     – only for interfaces
     – nasty for more than 1 method
PETER KOFLER, CODE-COP.ORG                 FANATIC ABOUT CODE QUALITY




          Dynamic Mock Frameworks
  • EasyMock, jMock, ... (in fact since 1.5)
  • mock interfaces (Proxy)
  • mock non final classes (cglib)
  import static org.easymock.EasyMock.*;

  SomeInt mock = createMock(SomeInt.class);
  expect(mock.someMethod("param")).andReturn(42);
  replay(mock);
  // run the test which calls someMethod once
  verify(mock);
PETER KOFLER, CODE-COP.ORG                                                          FANATIC ABOUT CODE QUALITY




                             Mocks in Spring
  • IoC make it easy, just set the mock
  • combination of context/mocks
        – needs mocks inside Spring:
  <bean id="someMock" class="org.easymock.EasyMock"
        factory-method="createMock">
    <constructor-arg index="0" value="...SomeBean" />
  </bean>



       see http://satukubik.com/2007/12/21/spring-tips-initializing-bean-using-easymock-for-unit-test/
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                             Enabling Mocking
  • Program to interfaces, not implementations.
        – interfaces are easier to mock
  • Law of Demeter
        – style guideline
        – “Only talk to your immediate friends.”
        – calling methods on objects you get from other
          collaborators is trouble - your mocks must
          expose internal state through these methods
PETER KOFLER, CODE-COP.ORG                       FANATIC ABOUT CODE QUALITY




                             Limits of Mocking
  • behave accordingly to your expectations
        – Do you know the mocked class good enough?
  • complex mocks are error prone
        – e.g. state machines
        – refactor using Law of Demeter
  • replace the right classes
        – not the tested ones!
        – focus on what goes inside than what comes out
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                             Testing Timings
  • timings (e.g. timeouts) are difficult
        – timing/scheduling is not guaranteed
        – short timings almost always fail
        – long timings slow down the execution
  • You will never get it right!
        – esp. not for Windows and Unix at same time
          mock the timer
PETER KOFLER, CODE-COP.ORG                            FANATIC ABOUT CODE QUALITY




                         Singletons are evil!
  • most overused design pattern
        – typical: public static Instance getInstance()
        – static methods (“mingletons”), e.g.
             System.currentTimeMillis()
        – static fields (“fingletons”)
        – ThreadLocal
  • most likely you have too many of them
       see http://c2.com/cgi/wiki?SingletonsAreEvil
PETER KOFLER, CODE-COP.ORG                        FANATIC ABOUT CODE QUALITY




                             Testing Singletons
  • problems for testing
        –    evil
        –    unknown dependencies
        –    initialisation often expensive (fixture)
        –    side effects in same class loader
        –    concurrency issues when testing in parallel
        –    can’t mock
PETER KOFLER, CODE-COP.ORG                        FANATIC ABOUT CODE QUALITY




   Testing Singletons “Brute Force”
  • straight forward
        – (fake) initialise singleton in fixture (setUp)
        – use Ant’s forkmode=“perTest”
        – slow2
  • if singletons can be reset
        – cleanup singleton in shutDown
        – make sure double initialisation fails
        – still slow, still no mock
PETER KOFLER, CODE-COP.ORG                               FANATIC ABOUT CODE QUALITY




              Testing Singletons “AOP”
  • context-sensitive modification with
    AspectJ
  • returning a mock instead of proceeding
    (around advice)
  • per-test-case basis (using various pointcuts)
        – execution(public void SomeTest.test*())
        – cflow(inTest()) && //other conditions
  • see http://www.ibm.com/developerworks/java/library/j-aspectj2/

  • mock , but .aj files get nasty
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                         Refactor Singletons
  • for new code - avoid singletons
  • refactor
        – pass singleton instance from outside to certain
          methods as argument, mock it
        – create a global registry for all singletons, which
          is the only singleton then, register mocks there
        – make whole singleton a Spring bean with
          singleton scope, mock it
PETER KOFLER, CODE-COP.ORG               FANATIC ABOUT CODE QUALITY




                       Testing J2EE - JNDI
  • use mocks like Simple-JNDI or MockEJB
  protected void setUp() throws Exception {
    super.setUp();
    MockContextFactory.setAsInitial();
    new InitialContext().bind("name", stuff);
  }
  protected void tearDown() throws Exception {
    MockContextFactory.revertSetAsInitial();
    super.tearDown();
  }
PETER KOFLER, CODE-COP.ORG                   FANATIC ABOUT CODE QUALITY




                        Testing J2EE - JMS
  • use mock implementation like MockEJB
  • use in memory JMS like ApacheActiveMQ
  <bean id="factory" class="..ActiveMQConnectionFactory">
    <property name="brokerURL” value="vm://broker?
                    broker.persistent=false&amp;
                    broker.useJmx=false" />
  </bean>
  <bean id="queue" class="...command.ActiveMQQueue">
    <constructor-arg value="SomeQueue" />
  </bean>
PETER KOFLER, CODE-COP.ORG                 FANATIC ABOUT CODE QUALITY




                    Testing J2EE - Servlet
  • call them (HttpClient, HttpUnit)
        – needs deployment and running server 
        – integration test
        – beware GUI changes
  •    run them in container (Cactus)
  •    embedded server (Jetty ServletTester)
  •    mock container (ServletUnit of HttpUnit)
  •    mock/implement interfaces yourself
PETER KOFLER, CODE-COP.ORG                FANATIC ABOUT CODE QUALITY




                         Testing J2EE - EJB
  • embedded server (Glassfish) ?
        – all since EJB 3.1
  • run them in container (Cactus)
  • mock container (MockEJB)
  • using an aspect to replace EJB lookups

  • EJB 3.x are just POJOs 
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                             Agenda
  • JUnit Basics 
        – Test Methods, Assertions, Fixtures
  • Advanced Topics 
        – Privates, Mocks, Timings, Singletons, J2EE
  • Tuning
  • Code Coverage
  • JUnit Extensions
        – Tools, Scripting, JUnit and the Build
PETER KOFLER, CODE-COP.ORG                       FANATIC ABOUT CODE QUALITY




                   Tune Test Performance
  • profile test suite - it’s run very often!
  • Ant/JUnit report contains execution times
  • target longest running tests
        –    tune as any Java program (CPU, heap)
        –    mock expensive/slow objects
        –    avoid expensive set-up (e.g. Spring Context)
        –    move expensive set-up to @BeforeClass
PETER KOFLER, CODE-COP.ORG                FANATIC ABOUT CODE QUALITY




          Test Performance - Database
  • database access is slow
  • mock out database
        – difficult for complex queries
  • use embedded memory database
        – e.g. HyperSQL DataBase (HSQLDB), H2
        – beware of duplicating schema info
        – Hibernate’s import.sql
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




  DB/Integration Test Performance
  • with database more an integration test
        – no problem - we want to test this too
  •    don't use fixtures
  •    do not commit
  •    connection pool
  •    tune database access (as usual)
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                             Agenda
  • JUnit Basics 
        – Test Methods, Assertions, Fixtures
  • Advanced Topics 
        – Privates, Mocks, Timings, Singletons, J2EE
  • Tuning 
  • Code Coverage
  • JUnit Extensions
        – Tools, Scripting, JUnit and the Build
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                             Code Coverage
  • tracks comprehensiveness of tests
        – % of classes/methods/lines that got executed
        – identifies parts of program lacking tests
  • 85-90% is “good enough”
        – can’t reach 100% (catch-blocks etc.)
        – no need to test everything (getters etc.)
        – at least focus on core systems (business critical)
PETER KOFLER, CODE-COP.ORG                           FANATIC ABOUT CODE QUALITY




                      Code Coverage Tools
  • EMMA
        –    instrument classes offline or on the fly
        –    detects partial coverage (if/short circuit)
        –    Ant, Maven, Eclipse (EclEmma)
        –    even able to track Eclipse plugins
        –    also used in test staging to test the testers
  • Cobertura
  • etc.
PETER KOFLER, CODE-COP.ORG                             FANATIC ABOUT CODE QUALITY




                             “Don’t Be Fooled”
  • comprehensiveness  quality!
        – high coverage does not mean anything
        – tools like AgitarOne create it
  • see http://www.ibm.com/developerworks/java/library/j-cq01316/

  • “Test state coverage, not code coverage.”
       (Pragmatic Tip 65)
        – difficult to measure
  • Crap4J “metric”
PETER KOFLER, CODE-COP.ORG                            FANATIC ABOUT CODE QUALITY




                      Development Process
  • code test & class (or class & test)
  • run tests with EclEmma (or on build)
        –    all important methods executed?
        –    all relevant if-branches executed?
        –    most common error cases executed?
        –    just browse the report line by line...
PETER KOFLER, CODE-COP.ORG              FANATIC ABOUT CODE QUALITY




                     How to Get Coverage
  • difficult to add tests to an existing program
  • wasn’t written with testing in mind
  • better to write tests before
      Test Driven Development (TDD)
                Red/Green/Refactor

  • Design to Test (Pragmatic Tip 48)
But How To Test This?
PETER KOFLER, CODE-COP.ORG                                     FANATIC ABOUT CODE QUALITY




                             Legacy Code
  • ... is code without test. (Michael Feathers)
  • write test for new features
  • create tests for bug reports, then fix bugs
        – Find Bugs Once (Pragmatic Tip 66)
  • find insertion points/bring them under test
        – for more see “Working Effectively with Legacy Code”

  • refactor for testability (TestabilityExplorer)
        – see http://code.google.com/p/testability-explorer/
PETER KOFLER, CODE-COP.ORG                                FANATIC ABOUT CODE QUALITY




     But Management Won’t Let Me
  • Testing is a mindset - You have to want it.
  • A thoroughly tested program will take twice
    as long to produce as one that's not tested.
        – you need time to write tests
        – argue for it
        – or just lie 
               • hide time in your estimations
               • say the feature is not finished
               • write tests before, so you can’t finish without tests
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




                             Agenda
  • JUnit Basics 
        – Test Methods, Assertions, Fixtures
  • Advanced Topics 
        – Privates, Mocks, Timings, Singletons, J2EE
  • Tuning 
  • Code Coverage 
  • JUnit Extensions
        – Tools, Scripting, JUnit and the Build
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                    JUnit Extensions (e.g.)
  • DbUnit - database fixtures
  • HtmlUnit/HttpUnit - GUI-less browser
        – typical for functional/integration tests
  • JUnitPerf - measure performance
        – no ordinary unit test  different package
  • SWTBot - UI testing SWT/Eclipse
  • XMLUnit - XML asserts
PETER KOFLER, CODE-COP.ORG                               FANATIC ABOUT CODE QUALITY




   New Trend: Scripting Languages
  • “Testing is a scripting problem.”
        – dynamically typed, easier to write tests
  • “If I can write tests in a rapid manner, I can
    view their results quicker.” (Andy Glover)
  • need tight Java integration
  • e.g. using Groovy
        – GroovyTestCase extends TestCase
        – see http://www.ibm.com/developerworks/java/library/j-pg11094/
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




                   (J)Ruby Test::Unit
  • typical xUnit implementation
  • asserts like
        – assert_raise, assert_throws
  • advanced frameworks
        – JtestR - JRuby integration “so that running
          tests is totally painless to set up”
        – RSpec - Behaviour Driven Development
          framework for Ruby
PETER KOFLER, CODE-COP.ORG                 FANATIC ABOUT CODE QUALITY




                             ScalaTest
  • run JUnit in ScalaTest
        – with wrapper JUnit3WrapperSuite
  • run ScalaTest in JUnit (JUnit3Suite)
  • Specs - Behaviour Driven Development
  • ScalaCheck - property-based testing
        – automatic test case generation
        – specify("startsWith", (a:String,
          b:String) => (a+b).startsWith(a) )
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                        JUnit and The Build
  • the build must be fast (max. 10 minutes)
        – typically tests take large part of build time
        – monitor and tune test performance
  • execute tests from very beginning (or die)
  • make it impossible to deploy failed builds
  • programmatically assessing and fixing
    blame is a bad practice
PETER KOFLER, CODE-COP.ORG                    FANATIC ABOUT CODE QUALITY




                             Ant and Maven
  • Integration 
  • Ant < 1.7
        – add junit.jar to Ant boot classpath (lib)
        – each JUnit 4.x test class needs to be wrapped as
          a JUnit 3.8 suite with JUnit4TestAdapter
  • Maven
        – Hudson (uses Maven) continues if tests failed
        – build is marked as unstable
PETER KOFLER, CODE-COP.ORG                     FANATIC ABOUT CODE QUALITY




      Running JUnit in Parallel (Ant)
  • causes lots of problems 
        – separate class loaders - more PermSpace
        – same class loader - singletons
             (<junit … reloading="false">)
        – separate VM instances = high start-up cost
             (<junit ... fork="yes">)
             forkmode="perBatch" only since Ant 1.6.2
        – load balancing of worker threads/VMs?
        – database race conditions, dead locks, ...
PETER KOFLER, CODE-COP.ORG                       FANATIC ABOUT CODE QUALITY




                             Distributed JUnit
  • not all tests are the same...
  • small/fast tests should not be distributed
        – distributing takes up to 90% of total time
  •    performs best with a few long running tests
  •    Distributed JUnit (ComputeFarm & Jini)
  •    GridGain’s JunitSuiteAdapter
  •    commercial build servers/agent technology
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                       Some Good Books...
                    • Kent Beck - Test Driven
                      Development. By Example (2002)



                    • Andy Hunt, Dave Thomas -
                      Pragmatic Unit Testing in Java with
                      JUnit (2003)
PETER KOFLER, CODE-COP.ORG                      FANATIC ABOUT CODE QUALITY




                       Some Good Books...
                    • Klaus Meffert - JUnit Profi-Tipps
                      (2006)



                    • Michael Feathers - Working
                      Effectively with Legacy Code
                      (2007)
Now go and write some tests!
PETER KOFLER, CODE-COP.ORG         FANATIC ABOUT CODE QUALITY




                             Q&A
  • Thank you for listening.
PETER KOFLER, CODE-COP.ORG             FANATIC ABOUT CODE QUALITY




                                Peter Kofler

                             @codecopkofler

             www.code-cop.org
PETER KOFLER, CODE-COP.ORG                                    FANATIC ABOUT CODE QUALITY




                             Image Sources
             •    http://rubystammtisch.at/



             •    http://www.flickr.com/photos/43442082@N00/296362882/



             •    http://www.flickr.com/photos/eszter/144393616/


             •    http://www.flickr.com/photos/sneddon/2413980712/
PETER KOFLER, CODE-COP.ORG                                    FANATIC ABOUT CODE QUALITY




                             Image Sources
             •    http://www.flickr.com/photos/paopix/184238679/




             •    http://www.flickr.com/photos/teotwawki/164966631/


             •    http://www.flickr.com/photos/paulk/3166328163/


             •    http://www.flickr.com/photos/otolithe/1831281833/
PETER KOFLER, CODE-COP.ORG                                    FANATIC ABOUT CODE QUALITY




                             Image Sources
             •    http://www.flickr.com/photos/rainforestactionnetwork/420117729/


             •    http://www.flickr.com/photos/ppdigital/2054989998/




             •    http://www.flickr.com/photos/good_day/48642035/

             •    http://www.flickr.com/photos/shelley13/2653029303/

Weitere ähnliche Inhalte

Was ist angesagt?

Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
Alex Su
 
May: Automated Developer Testing: Achievements and Challenges
May: Automated Developer Testing: Achievements and ChallengesMay: Automated Developer Testing: Achievements and Challenges
May: Automated Developer Testing: Achievements and Challenges
TriTAUG
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
Dror Helper
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
Steven Mak
 
PHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An IntroductionPHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An Introduction
alexmace
 

Was ist angesagt? (20)

Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
May: Automated Developer Testing: Achievements and Challenges
May: Automated Developer Testing: Achievements and ChallengesMay: Automated Developer Testing: Achievements and Challenges
May: Automated Developer Testing: Achievements and Challenges
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
Taking a Test Drive: iOS Dev UK guide to TDD
Taking a Test Drive: iOS Dev UK guide to TDDTaking a Test Drive: iOS Dev UK guide to TDD
Taking a Test Drive: iOS Dev UK guide to TDD
 
Client Side Unit Testing
Client Side Unit TestingClient Side Unit Testing
Client Side Unit Testing
 
Google test training
Google test trainingGoogle test training
Google test training
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit testing on embedded target with C++Test
Unit testing on embedded  target with C++TestUnit testing on embedded  target with C++Test
Unit testing on embedded target with C++Test
 
Design For Testability
Design For TestabilityDesign For Testability
Design For Testability
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
How penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skillsHow penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skills
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ?
 
Plone Testing Tools And Techniques
Plone Testing Tools And TechniquesPlone Testing Tools And Techniques
Plone Testing Tools And Techniques
 
PHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An IntroductionPHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An Introduction
 

Andere mochten auch

Andere mochten auch (9)

Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)
 
Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)Coding Dojo: Asynchronous Clock-In (2016)
Coding Dojo: Asynchronous Clock-In (2016)
 
Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)Designing Test Cases for the Gilded Rose Kata v3 (2016)
Designing Test Cases for the Gilded Rose Kata v3 (2016)
 
Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)Clean Readable Specifications (ETC 2016)
Clean Readable Specifications (ETC 2016)
 
Code Retreat Venice (2016)
Code Retreat Venice (2016)Code Retreat Venice (2016)
Code Retreat Venice (2016)
 
Mob Programming (2016)
Mob Programming (2016)Mob Programming (2016)
Mob Programming (2016)
 
Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)Refactoring the Tennis Kata v2 (2016)
Refactoring the Tennis Kata v2 (2016)
 
Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)
 
Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)Coding Dojo: Data Munging (2016)
Coding Dojo: Data Munging (2016)
 

Ähnlich wie Practical (J)Unit Testing (2009)

Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
Peter Kofler
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)
Peter Kofler
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
TO THE NEW | Technology
 

Ähnlich wie Practical (J)Unit Testing (2009) (20)

Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
 
Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)Code Quality Assurance with PMD (2004)
Code Quality Assurance with PMD (2004)
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)
 
CBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBoxCBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBox
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
 
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAPABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
 
Outside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDOutside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDD
 
Ch11lect1 ud
Ch11lect1 udCh11lect1 ud
Ch11lect1 ud
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
The Test way
The Test wayThe Test way
The Test way
 

Mehr von Peter Kofler

Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Peter Kofler
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Peter Kofler
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
Peter Kofler
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
Peter Kofler
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
Peter Kofler
 

Mehr von Peter Kofler (18)

Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)
 
Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)
 
JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)JUnit Boot Camp (GeeCON 2016)
JUnit Boot Camp (GeeCON 2016)
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)
 
Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)
 
GDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran CanariaGDCR15 in Las Palmas, Gran Canaria
GDCR15 in Las Palmas, Gran Canaria
 
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)
 
Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)
 
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
 
Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)Code Refactoring - Live Coding Demo (JavaDay 2014)
Code Refactoring - Live Coding Demo (JavaDay 2014)
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Practical (J)Unit Testing (2009)

  • 1. Practical Unit Testing: “Good programmers write code, great programmers write tests.” Java Student User Group 2009 Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.
  • 2. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Peter Kofler • Ph.D. in Applied Math • Java developer since 1999 • fanatic about code quality • appointed ‘Code Cop’ in 2006 • Senior Software Engineer at s-IT Solutions
  • 3. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Agenda • JUnit Basics – Test Methods, Assertions, Fixtures • Advanced Topics – Privates, Mocks, Timings, Singletons, J2EE • Tuning • Code Coverage • JUnit Extensions – Tools, Scripting, JUnit and the Build
  • 4. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY A Little Survey... • Who knows xUnit? • Who knows JUnit 4? • Who ever wrote a unit test? • Who writes tests and tries to write them first? • Who checks the coverage? • Who ever produced a bug?
  • 5. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY We Make Mistakes • at least I do...  • number of bugs proportional loc – 2 bugs per 1.000 loc (7 or even more...) – 1 bug per 10.000 loc in critical software • be paranoid when you write software – Assume you have lots of bugs. – Try to find these bugs aggressively.
  • 6. I find your lack of tests disturbing.
  • 7. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Wait - We Have Tests • almost every project has some “tests” • almost all of them are useless  – experiments how to use some library – main methods, waiting for user input, ... – tests that initialise the whole application and check nothing – tests that fail since long, etc. No You Don’t!
  • 8. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY JUnit • a unit testing framework • active, dynamic black-box tests – some call it white-box tests (tbd) • works best with a number of small tests • You should know it (no excuses!) – You should use it (no excuses!) – I will not explain it here  www.junit.org
  • 9. “Keep the bar green to keep the code clean”
  • 10. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Test Methods • unit test tests the methods of a single class • test case tests the response of a single method to a particular set of inputs – multiple test cases for a single method – public void testMethod() or @Test – test methods should be short, simple – tests without test methods are pointless  Findbugs and PMD
  • 11. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Assertions • Don’t do any output from your unit tests! • check expectations programmatically – assertEquals,assertNull,assertTrue,... – test method without assert is pointless (PMD) – one test method - one assertion (tbd) • some work around PMD with assertTrue(true)  PMD: UnnecessaryBooleanAssertion • test runner reports failure on each test
  • 12. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Proper Assertions • add messages to asserts (tbd) ( PMD) • assertTrue(a.equals(b)) no message, better use assertEquals(a,b) ( PMD) • assert in Thread.run() not noticed ( Findbugs: IJU_...) • assert float in ranges of precision: assertEquals(expected, actual, 5*Math.ulp(expected))
  • 13. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Assertions (JUnit 4) • assertArrayEquals(.) for atom arrays and Object • but assertEquals(int,int) removed – not needed any more (auto boxing) – problems with mixed params, e.g. (int,byte) – JUnit 3: promoted to (int,int), succeeds – JUnit 4: boxed to (Integer,Byte), fails
  • 14. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Asserting Exceptions • JUnit 3 try-catch code: try { // code that should cause an exception fail("no exception occurred"); } catch (SomeException success) { // check exception type/parameters • JUnit 4: @Test(expected)annotation: @Test(expected=SomeException.class) public void testThrows() { // code that should cause an exception
  • 15. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Fixtures (JUnit 3) • sets up data needed to run tests • JUnit 3: setUp(), tearDown() – don’t forget to call super.setUp() first – don’t forget to call super.tearDown() last – don’t forget it (!) – Findbugs: IJU_SETUP_NO_SUPER,IJU_... • for fixture in JUnit 3.x that runs only once, use the TestSetup decorator
  • 16. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY JUnit 3 TestSetup Decorator public class TheTest extends TestCase { // test methods ... public static Test suite() { return new TestSetup(new TestSuite(TheTest.class)) { protected void setUp() throws Exception { super.setUp(); // set-up code called only once } protected void tearDown() throws Exception { // tear-down code called only once super.tearDown(); } };
  • 17. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Fixtures • JUnit 4: @Before, @After – run methods of super classes – only once: @BeforeClass, @AfterClass • test data in database is problematic – test has to insert its own preconditions – large data sets  DbUnit • Remember: Test data is more likely to be wrong than the tested code!
  • 18. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Test Code Organisation • test code loc ~ functional code loc • same quality as production code – always built with test code – execute tests as soon/often as possible • parallel package hierarchy – no *.test sub-packages! (tbd) – folder test (simple), src/test/java (Maven) – package-access!
  • 19. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Test Class Organisation • create your own base test case(s) – named *TestCase or *TC (not *Test) – common methods, initialisation code – custom asserts, named assert* (PMD) • name test classes <tested class>Test
  • 20.
  • 21. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Agenda • JUnit Basics  – Test Methods, Assertions, Fixtures • Advanced Topics – Privates, Mocks, Timings, Singletons, J2EE • Tuning • Code Coverage • JUnit Extensions – Tools, Scripting, JUnit and the Build
  • 22. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing Private Data • “Wishing for White Box Testing (i.e. check a private field) is not a testing problem, it is a design problem.” – If you want to check internals - improve design. • if you have to: – Reflection: member.setAccessible(true)
  • 23. Mocks
  • 24. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY When to Use Mocks • To have a “real” unit test (cut dependencies) • “It is much simpler to simulate behaviour than it is to recreate that behaviour.” • use a mock when the real object is – non-deterministic (e.g. current time) – problematic during execution (e.g. user input) – difficult to trigger (e.g. network error) – not existing yet (team collaboration)
  • 25. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY How to Mock an Object • by hand – implement its interface (Eclipse Ctrl-1) – subclass it (beware complex constructors) • java.lang.reflect.Proxy – since 1.3 – only for interfaces – nasty for more than 1 method
  • 26. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Dynamic Mock Frameworks • EasyMock, jMock, ... (in fact since 1.5) • mock interfaces (Proxy) • mock non final classes (cglib) import static org.easymock.EasyMock.*; SomeInt mock = createMock(SomeInt.class); expect(mock.someMethod("param")).andReturn(42); replay(mock); // run the test which calls someMethod once verify(mock);
  • 27. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Mocks in Spring • IoC make it easy, just set the mock • combination of context/mocks – needs mocks inside Spring: <bean id="someMock" class="org.easymock.EasyMock" factory-method="createMock"> <constructor-arg index="0" value="...SomeBean" /> </bean> see http://satukubik.com/2007/12/21/spring-tips-initializing-bean-using-easymock-for-unit-test/
  • 28. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Enabling Mocking • Program to interfaces, not implementations. – interfaces are easier to mock • Law of Demeter – style guideline – “Only talk to your immediate friends.” – calling methods on objects you get from other collaborators is trouble - your mocks must expose internal state through these methods
  • 29. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Limits of Mocking • behave accordingly to your expectations – Do you know the mocked class good enough? • complex mocks are error prone – e.g. state machines – refactor using Law of Demeter • replace the right classes – not the tested ones! – focus on what goes inside than what comes out
  • 30. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing Timings • timings (e.g. timeouts) are difficult – timing/scheduling is not guaranteed – short timings almost always fail – long timings slow down the execution • You will never get it right! – esp. not for Windows and Unix at same time  mock the timer
  • 31.
  • 32. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Singletons are evil! • most overused design pattern – typical: public static Instance getInstance() – static methods (“mingletons”), e.g. System.currentTimeMillis() – static fields (“fingletons”) – ThreadLocal • most likely you have too many of them see http://c2.com/cgi/wiki?SingletonsAreEvil
  • 33. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing Singletons • problems for testing – evil – unknown dependencies – initialisation often expensive (fixture) – side effects in same class loader – concurrency issues when testing in parallel – can’t mock
  • 34. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing Singletons “Brute Force” • straight forward – (fake) initialise singleton in fixture (setUp) – use Ant’s forkmode=“perTest” – slow2 • if singletons can be reset – cleanup singleton in shutDown – make sure double initialisation fails – still slow, still no mock
  • 35. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing Singletons “AOP” • context-sensitive modification with AspectJ • returning a mock instead of proceeding (around advice) • per-test-case basis (using various pointcuts) – execution(public void SomeTest.test*()) – cflow(inTest()) && //other conditions • see http://www.ibm.com/developerworks/java/library/j-aspectj2/ • mock , but .aj files get nasty
  • 36. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Refactor Singletons • for new code - avoid singletons • refactor – pass singleton instance from outside to certain methods as argument, mock it – create a global registry for all singletons, which is the only singleton then, register mocks there – make whole singleton a Spring bean with singleton scope, mock it
  • 37. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing J2EE - JNDI • use mocks like Simple-JNDI or MockEJB protected void setUp() throws Exception { super.setUp(); MockContextFactory.setAsInitial(); new InitialContext().bind("name", stuff); } protected void tearDown() throws Exception { MockContextFactory.revertSetAsInitial(); super.tearDown(); }
  • 38. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing J2EE - JMS • use mock implementation like MockEJB • use in memory JMS like ApacheActiveMQ <bean id="factory" class="..ActiveMQConnectionFactory"> <property name="brokerURL” value="vm://broker? broker.persistent=false&amp; broker.useJmx=false" /> </bean> <bean id="queue" class="...command.ActiveMQQueue"> <constructor-arg value="SomeQueue" /> </bean>
  • 39. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing J2EE - Servlet • call them (HttpClient, HttpUnit) – needs deployment and running server  – integration test – beware GUI changes • run them in container (Cactus) • embedded server (Jetty ServletTester) • mock container (ServletUnit of HttpUnit) • mock/implement interfaces yourself
  • 40. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Testing J2EE - EJB • embedded server (Glassfish) ? – all since EJB 3.1 • run them in container (Cactus) • mock container (MockEJB) • using an aspect to replace EJB lookups • EJB 3.x are just POJOs 
  • 41.
  • 42. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Agenda • JUnit Basics  – Test Methods, Assertions, Fixtures • Advanced Topics  – Privates, Mocks, Timings, Singletons, J2EE • Tuning • Code Coverage • JUnit Extensions – Tools, Scripting, JUnit and the Build
  • 43. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Tune Test Performance • profile test suite - it’s run very often! • Ant/JUnit report contains execution times • target longest running tests – tune as any Java program (CPU, heap) – mock expensive/slow objects – avoid expensive set-up (e.g. Spring Context) – move expensive set-up to @BeforeClass
  • 44. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Test Performance - Database • database access is slow • mock out database – difficult for complex queries • use embedded memory database – e.g. HyperSQL DataBase (HSQLDB), H2 – beware of duplicating schema info – Hibernate’s import.sql
  • 45. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY DB/Integration Test Performance • with database more an integration test – no problem - we want to test this too • don't use fixtures • do not commit • connection pool • tune database access (as usual)
  • 46.
  • 47. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Agenda • JUnit Basics  – Test Methods, Assertions, Fixtures • Advanced Topics  – Privates, Mocks, Timings, Singletons, J2EE • Tuning  • Code Coverage • JUnit Extensions – Tools, Scripting, JUnit and the Build
  • 48. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Code Coverage • tracks comprehensiveness of tests – % of classes/methods/lines that got executed – identifies parts of program lacking tests • 85-90% is “good enough” – can’t reach 100% (catch-blocks etc.) – no need to test everything (getters etc.) – at least focus on core systems (business critical)
  • 49. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Code Coverage Tools • EMMA – instrument classes offline or on the fly – detects partial coverage (if/short circuit) – Ant, Maven, Eclipse (EclEmma) – even able to track Eclipse plugins – also used in test staging to test the testers • Cobertura • etc.
  • 50. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY “Don’t Be Fooled” • comprehensiveness  quality! – high coverage does not mean anything – tools like AgitarOne create it • see http://www.ibm.com/developerworks/java/library/j-cq01316/ • “Test state coverage, not code coverage.” (Pragmatic Tip 65) – difficult to measure • Crap4J “metric”
  • 51. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Development Process • code test & class (or class & test) • run tests with EclEmma (or on build) – all important methods executed? – all relevant if-branches executed? – most common error cases executed? – just browse the report line by line...
  • 52. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY How to Get Coverage • difficult to add tests to an existing program • wasn’t written with testing in mind • better to write tests before  Test Driven Development (TDD) Red/Green/Refactor • Design to Test (Pragmatic Tip 48)
  • 53. But How To Test This?
  • 54. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Legacy Code • ... is code without test. (Michael Feathers) • write test for new features • create tests for bug reports, then fix bugs – Find Bugs Once (Pragmatic Tip 66) • find insertion points/bring them under test – for more see “Working Effectively with Legacy Code” • refactor for testability (TestabilityExplorer) – see http://code.google.com/p/testability-explorer/
  • 55. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY But Management Won’t Let Me • Testing is a mindset - You have to want it. • A thoroughly tested program will take twice as long to produce as one that's not tested. – you need time to write tests – argue for it – or just lie  • hide time in your estimations • say the feature is not finished • write tests before, so you can’t finish without tests
  • 56.
  • 57. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Agenda • JUnit Basics  – Test Methods, Assertions, Fixtures • Advanced Topics  – Privates, Mocks, Timings, Singletons, J2EE • Tuning  • Code Coverage  • JUnit Extensions – Tools, Scripting, JUnit and the Build
  • 58. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY JUnit Extensions (e.g.) • DbUnit - database fixtures • HtmlUnit/HttpUnit - GUI-less browser – typical for functional/integration tests • JUnitPerf - measure performance – no ordinary unit test  different package • SWTBot - UI testing SWT/Eclipse • XMLUnit - XML asserts
  • 59. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY New Trend: Scripting Languages • “Testing is a scripting problem.” – dynamically typed, easier to write tests • “If I can write tests in a rapid manner, I can view their results quicker.” (Andy Glover) • need tight Java integration • e.g. using Groovy – GroovyTestCase extends TestCase – see http://www.ibm.com/developerworks/java/library/j-pg11094/
  • 60. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY (J)Ruby Test::Unit • typical xUnit implementation • asserts like – assert_raise, assert_throws • advanced frameworks – JtestR - JRuby integration “so that running tests is totally painless to set up” – RSpec - Behaviour Driven Development framework for Ruby
  • 61. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ScalaTest • run JUnit in ScalaTest – with wrapper JUnit3WrapperSuite • run ScalaTest in JUnit (JUnit3Suite) • Specs - Behaviour Driven Development • ScalaCheck - property-based testing – automatic test case generation – specify("startsWith", (a:String, b:String) => (a+b).startsWith(a) )
  • 62. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY JUnit and The Build • the build must be fast (max. 10 minutes) – typically tests take large part of build time – monitor and tune test performance • execute tests from very beginning (or die) • make it impossible to deploy failed builds • programmatically assessing and fixing blame is a bad practice
  • 63. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Ant and Maven • Integration  • Ant < 1.7 – add junit.jar to Ant boot classpath (lib) – each JUnit 4.x test class needs to be wrapped as a JUnit 3.8 suite with JUnit4TestAdapter • Maven – Hudson (uses Maven) continues if tests failed – build is marked as unstable
  • 64. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Running JUnit in Parallel (Ant) • causes lots of problems  – separate class loaders - more PermSpace – same class loader - singletons (<junit … reloading="false">) – separate VM instances = high start-up cost (<junit ... fork="yes">) forkmode="perBatch" only since Ant 1.6.2 – load balancing of worker threads/VMs? – database race conditions, dead locks, ...
  • 65. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Distributed JUnit • not all tests are the same... • small/fast tests should not be distributed – distributing takes up to 90% of total time • performs best with a few long running tests • Distributed JUnit (ComputeFarm & Jini) • GridGain’s JunitSuiteAdapter • commercial build servers/agent technology
  • 66.
  • 67. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Some Good Books... • Kent Beck - Test Driven Development. By Example (2002) • Andy Hunt, Dave Thomas - Pragmatic Unit Testing in Java with JUnit (2003)
  • 68. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Some Good Books... • Klaus Meffert - JUnit Profi-Tipps (2006) • Michael Feathers - Working Effectively with Legacy Code (2007)
  • 69. Now go and write some tests!
  • 70. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Q&A • Thank you for listening.
  • 71. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Peter Kofler @codecopkofler www.code-cop.org
  • 72. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Image Sources • http://rubystammtisch.at/ • http://www.flickr.com/photos/43442082@N00/296362882/ • http://www.flickr.com/photos/eszter/144393616/ • http://www.flickr.com/photos/sneddon/2413980712/
  • 73. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Image Sources • http://www.flickr.com/photos/paopix/184238679/ • http://www.flickr.com/photos/teotwawki/164966631/ • http://www.flickr.com/photos/paulk/3166328163/ • http://www.flickr.com/photos/otolithe/1831281833/
  • 74. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY Image Sources • http://www.flickr.com/photos/rainforestactionnetwork/420117729/ • http://www.flickr.com/photos/ppdigital/2054989998/ • http://www.flickr.com/photos/good_day/48642035/ • http://www.flickr.com/photos/shelley13/2653029303/