unit testing that sucks less - oracle...thoughtworks neal ford software architect / meme wr angle r...
TRANSCRIPT
ThoughtWorksThoughtWorks
NEAL FORD software architect / meme wrangler
ThoughtWorks
[email protected] 3003 Summit Boulevard, Atlanta, GA 30319
www.nealford.com
www.thoughtworks.com
blog: memeagora.blogspot.com
twitter: neal4d
unit testing that sucks less: small things that make a
big difference
housekeepingask questions anytime
download slides from nealford.com
download samples from github.com/nealford
groovymockrunner
jruby mockingjtestr & rspec
easyb
jesterinfinitesthamcrest
what i cover
http://code.google.com/p/hamcrest/
the test target
the old way
thou shalt assert equals expected and f.factors
included in junit 4.x, addable for junit 3.x
fluent interface matchers for xUnit
syntactic sugar for standard matchers
make assertions suck less
not a testing framework
hamcrest matchers
hamcrest: equalTo
hamcrest: is(equalTo...
hamcrest: is
hamcrest: anything
hamcrest: anyOf
Sucks
Less!
http://code.google.com/p/infinitest/
it picks tests intelligently
continuous test runner for junit tests
whenever you change a class, infinitest runs tests for you
plug-in for eclipse or intellij
compiler-like verification for unit tests
configuring intellij
infinitest in intellij
select the "Available Software" tab, and hit "Add Site..."
copy the update url to your clipboard: http://www.infinitest.org/eclipse-update
In eclipse, choose the Help->Software Updates menu item
choose the appropriate version of Infinitest by checking the box to the left, & hit "Install"
paste the update URL into the "Location" field, and hit "OK"
configuring eclipse
infinitest in eclipse
Sucks
Less!
http://jester.sourceforge.net/
makes changes to your tests
test permutation framework
jester:
hopes the tests fail
rebuilds the code
BUILD_COMMAND is the command to build your project and run tests
only run jester on a copy of your files!
java jester.TestTester BUILD_COMMAND SOURCE_DIRECTORY
produces an xml report
SOURCE_DIRECTORY is the source directory for your tests
running jester
python makeWebView.py
jester produces an xml output file
includes a python script to output each changed file to html
shows what was changed
produces a friendly report
reporting results
poor tests
improved code
Sucks
Less!
http://mockrunner.sourceforge.net/
includes a JDBC, a JMS and a JCA test framework
lightweight framework for unit testing applications in the J2EE environment
supports servlets, filters, tag classes and Struts actions and forms
write very sophisticated unit-tests for J2EE based applications without any overhead
extends junit and simulates the necessary behavior without calling the real infrastructure
Mockrunner
testing struts actions
extending BasicActionTestCaseAdapter
delegator
testing servlets
testing HTML using JDOM
testing jdbc
testing transactions
mocking JMS
creating the fixture
the test
stubbing via inheritance
mockrunner is fast!
Sucks
Less!
http://unitils.org/
mock objects
open source set of utility classes to make typical java scenarios easier to test
offers support to hibernate, spring, JPA
spring integration
persistence layer testing support
assertion utilities
asserting user1 == user2
testing identityequals
method in User
what is tested?
more comprehensive
reflection assertions
loops over all fields in both objects and compares their values using reflection
lenient assertions
dbUnit supportdbUnit files to be
loaded for this test
dbUnit support
this data will be loaded prior to test run
firstname == null
hibernate support
injection of Spring beans in unit tests
sometimes useful to have spring around during testing
management of ApplicationContext configuration
reference the Unitils DataSource in Spring configuration
make use of a hibernate SessionFactory configured in Spring
spring support
spring support
ApplicationContext
injection
mocksauto creation
of mocks
expectations
verification
Sucks
Less!
http://code.google.com/p/spock/
nicely detailed error messages
testing & specification framework for Java & Groovy
highly expressive specification language
compatible with junit test runner
setup - stimulus - response - cleanup
spock
spock test specification
junit test runner interface
test classtest
stimulus
response
blocks & outcomes
setup
simple statements
of fact
pretty failures!
junit vs. spock
Sucks
Less!
tdd private methods?
solution #1:
make them all public(or package) scope
solution #2:
use reflection
reflection helpers
groovy
reflection
dirty secret:private is ignored!
technically, a bug......no great hurry to fix it (insanely useful!)
mocking
mocking in
FactorsFinder class harvests factors
Classifier uses external factors
changes:
Finder
FactorsFinder
Finder
FactorsFinder
Classifier
jmock
Sucks
!
mocking with
java-like syntax
factors :factors :
factors()
Finder
idiomatic
Sucks
Less!
http://jtestr.codehaus.org/
testing java with jruby
the java part
testing fill()
jmock
mocha
Sucks
Less!
what does it take???
jmock vs mocha loc
0
4,000
8,000
12,000
16,000
20,000
17,152
2,245JMock
mocha
jmock has 7.5 times as many lines of code
jmock vs mocha cc
0
600
1,200
1,800
2,400
3,000
2,349
323JMock
mocha
jmock has 7.2 times the complexity of mocha
...that execute
behavior driven development framework
allows you to create readable specifications...
inspiration for tons of similar frameworks
inspired by JBehave
rspec
specification
pretty results
Sucks
Less!
ThoughtWorks
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 License.
http://creativecommons.org/licenses/by-sa/3.0/us/
?’splease fill out the session evaluationssamples at github.com/nealford
NEAL FORD software architect / meme wrangler
ThoughtWorks
[email protected] 3003 Summit Boulevard, Atlanta, GA 30319
www.nealford.com
www.thoughtworks.com
blog: memeagora.blogspot.com
twitter: neal4d