23. JUnit
public class UriTest {
@Test
public void absoluteUriParamIsReturned() {
URI absoluteUri =
URI.create("http://www.example.com");
URI resolvedUri = resolveUriAgainstBase(
"http://localhost:8080", absoluteUri)
assertEquals(absoluteUri, resolvedUri);
}
}
Thursday, October 22, 2009
29. Unit Testing Traps
• False Positives
• False Negatives
• Flapping
Thursday, October 22, 2009
30. Non-Test
• Doesn’t test what it appears to test
• No assertion
• Passes when code is broken
• Solutions:
• Test-Driven Development
• IDE inspections, PMD, FindBugs
Thursday, October 22, 2009
31. Missing Test
• Doesn’t test full range of inputs
• Boundary conditions, special cases
• Passes when code is broken
• Solutions:
• Clover
• JUnit Theories
Thursday, October 22, 2009
32. Coverage as a Crutch
• Over-reliance on Clover
• Misses code paths, boundary cases
• Passes when code is broken
• Solutions:
• Your brain
• Peer review
Thursday, October 22, 2009
33. Side-Effects
• Produces or relies on side-effects
• Maintains state between runs
• Fails inconsistently
• Solutions:
• Mock objects
• @Before/@After
Thursday, October 22, 2009
34. Over Constraint
• Tests implementation details
• Overuse of mocks
• Fails when code is not broken
• Solutions:
• Stubs/fakes
• Refactoring
Thursday, October 22, 2009
37. What is Integration
Testing?
• Tests full system
• Tests each supported configuration
• Runs in simulated production environment
• Many types: functional, load, stress, etc.
Thursday, October 22, 2009
39. How to Integration Test
• Set up
• Execute
• Verify
• Clean up
Thursday, October 22, 2009
40. How to Integration Test
a Web Application
• Deploy your plugin or application
• Load data
• Simulate or script a web browser
• Parse the HTML to verify results
• Clean up
Thursday, October 22, 2009
50. Incomplete
• Doesn’t test the full system
• JavaScript disabled, ignores browsers or app servers
• Passes when code is broken
• Solutions:
• HtmlUnit
• Selenium
• Cargo
• atlas-integration-test --container
Thursday, October 22, 2009
51. Entangled
• Each test covers too much functionality
• Requires lots of setup to test each feature
• Bugs cause failures in tests for other features
• Solutions:
• Data loaders
• Remote APIs
• More specific test methods
• Domain-driven test frameworks
Thursday, October 22, 2009
52. Fragile
• Tests break due to UI changes
• Screen-scraping, complicated XPath
• Fails when not broken
• Solutions:
• Semantic markup (class and id attributes)
• Domain-driven test frameworks
Thursday, October 22, 2009
53. Timing or State
Dependencies
• Inconsistent results from one test run to another
• Concurrency bugs, no clean up, environment issues
• Solutions:
• @Before/@After
• @BeforeClass/@AfterClass
• Continuous Integration
• Blood, sweat, and tears
Thursday, October 22, 2009
54. Slow
• Tests take too long to run
• Increases length of feedback loop
• Solutions:
• Clover test optimization
• Maven profiles
• Bamboo build trees
Thursday, October 22, 2009
55. Build Trees
Clover-Optimized Faster
Unit Tests
All Unit Tests All Unit Tests
Java 5 Java 6 Slower
Integration Integration
Java 5/Tomcat 6 Java 6/Tomcat 6
Integration Integration Integration Integration
Java 5 Java 5 Java 6 Java 6
JBoss Tomcat 5.5 JBoss Tomcat 5.5
Thursday, October 22, 2009
56. Integration Testing Traps
• False Positives ←Test more
• False Negatives ←Framework
• Flapping ←Get to work
Thursday, October 22, 2009
57. Resources
http://j.mp/plugin-testing
Thursday, October 22, 2009