Intro to Mutation Testing (CON 1694)
slideshare.net/boyarsky
Twitter @jeanneboyarsky
Blog: http://www.selikoff.net
Jeanne Boyarsky
JavaOne - October 5, 2017
@jeanneboyarsky
About Me
15 years as
•Java Developer
•JUnit user
The Plan
1. About mutation testing
2. Live demo
3. A look at types of mutants
4. Profit???
@jeanneboyarsky
Why write tests?
@jeanneboyarsky
It used to
work!
How know if tests are good?
@jeanneboyarsky
If test coverage low
@jeanneboyarsky
Write more tests!
Not ready
for mutation
testing of class
If test coverage high
@jeanneboyarsky
All done!
Perfect code!
Or is it?Why is it
saying
nobody is
registered?
@jeanneboyarsky
Code Review!
@jeanneboyarsky
That’s a mutant!
@jeanneboyarsky
Too many mutants!
They’re
everywhere!
@jeanneboyarsky
PitClipse to the rescue!
@jeanneboyarsky
@jeanneboyarsky
PIT Integration Almost
JUnit 5
• Unofficial Fork:
https://github.com/tobiasstadler/pitest-junit5-plugin
• It’s coming:
https://github.com/hcoles/pitest/issues/284
https://github.com/hcoles/pitest/issues/398
@jeanneboyarsky
Easy to run
@jeanneboyarsky
The console flies by
@jeanneboyarsky
Wait, we just had 100% coverage
@jeanneboyarsky
We caught 5 mutants
@jeanneboyarsky
More detail
@jeanneboyarsky
Level up
@jeanneboyarsky
Now 23 survived
@jeanneboyarsky
Let’s try it!
@jeanneboyarsky
Observations
• JUnit tests must pass
• Fixing one may take care of more
• Found a bug
• Better tests
@jeanneboyarsky
Now with all mutators
@jeanneboyarsky
Observations
• Better tests
• Unused production code
@jeanneboyarsky
Examples of Conditional Mutants
@jeanneboyarsky
Conditionals Boundary <= vs <
Negative Conditionals <= vs >
Void Method Call Removes method calls
Examples of Math Mutants
@jeanneboyarsky
Math * vs /
Increments ++ vs --
Invert Negatives x vs -x
Examples of Other Mutants
@jeanneboyarsky
Return Values x vs x+1
Disabled by default (false
positives, duplicates or
crashes)
Constructor Calls
Inline Constants
Non Void Method Calls
Remove Conditionals
Experimental Member Variables
Switch
Tips for bigger projects
• Watch console to see if still running
• Reports on whole project – drill down/split up
• Exclude long running tests
• Experimental feature: incremental analysis
@jeanneboyarsky
Review
Code Coverage
Mutation Testing
Requirements
More info
• http://pitest.org/
• https://github.com/philglover/pitclipse
• https://dzone.com/articles/introduction-mutation
@jeanneboyarsky
@jeanneboyarsky

2017 JavaOne Mutation Testing Session

Editor's Notes

  • #4 https://cdn.pixabay.com/photo/2012/05/07/05/18/bug-48020_1280.png https://pixabay.com/p-303966/?no_redirect
  • #5 https://cdn.pixabay.com/photo/2012/05/07/05/18/bug-48020_1280.png https://pixabay.com/p-303966/?no_redirect
  • #6 http://www.eclemma.org/jacoco/trunk/coverage/
  • #7 https://pixabay.com/p-149251/?no_redirect
  • #9 https://pixabay.com/p-1300226/?no_redirect
  • #11 http://www.publicdomainpictures.net/pictures/180000/velka/mutant-female-41.jpg
  • #12 http://orig01.deviantart.net/920f/f/2015/013/a/8/teenage_mutant_ninja_turtles__2003__drawing__by_n0rvv3g14n_g1rl-d8drynd.jpg
  • #14 Image sources: https://commons.wikimedia.org/wiki/File:Antu_netbeans-ide.svg https://commons.wikimedia.org/wiki/File:Eclipse-SVG.svg https://commons.wikimedia.org/wiki/File:IntelliJ_IDEA_Logo.svg https://ant.apache.org https://maven.apache.org http://junit.org/junit4/
  • #23 Run initially Add 2 assertTrue and one assertFalse on tooManyAttendees – incorrectly to show failure with failing unit tests. Wait there’s a bug. Fix assertions and see down to three mutants In cancel test assertFalse on my registration now down to two mutants Then add assertTrue on Barry’s registration and see all mutants killed
  • #25 Change to all mutators Run initially and note that down from 23 to 7 – our tests are better Note that the wait list logic still isn’t tested enough. Add test for jeanne and maurice cancelling with barry and sai on wait list with four asserts 4 mutants left Add test for sai not on wait list so wait list empty 2 mutants left These two are interesting as they point to unused code – isRoomForMoreAttendees always true and don’t use company name (attendee map value)
  • #33 https://commons.wikimedia.org/wiki/File:Online_Survey_Icon_or_logo.svg