6. UNIT VS. INTEGRATION TESTING
Unit Testing
• Testing a unit in isolation
Integration Testing
• Testing the collaboration of
multiple units
https://leanpub.com/integrationtest/
6
10. UNIT + INTEGRATION TESTING
Take a prototype car on a
test drive having tested
only nuts and bolts?
Manufacture a car having
tested nuts and bolts but
without having tested it on
numerous test drives?
https://leanpub.com/integrationtest/
10
11. SYSTEM UNDER TEST
SUT is what get tested
Techniques from Unit
Testing can be re-used
• Dependency Injection
• Test doubles
https://leanpub.com/integrationtest/
11
13. TESTING IS ABOUT ROI
The larger the SUT
• The more fragile the test
• The less maintainable the
test
• The less the ROI
https://leanpub.com/integrationtest/
13
14. TESTING IS ABOUT ROI
Tests have to be
organized in a certain way
• The bigger the SUT
• The less the number of
tests
https://leanpub.com/integrationtest/
14
15. TESTING IS ABOUT ROI
Integration Testing
• Test nominal cases
https://leanpub.com/integrationtest/
15
20. INTEGRATION TESTS ARE STILL SLOW
Separating IT doesn’t
make them faster
But errors can be
uncovered faster
• Fail fast
• It will speed testing
https://leanpub.com/integrationtest/
20
21. HOW TO SEPARATE?
Depends on the build
tool
• Ant
• Maven
• Gradle
• Something else?
https://leanpub.com/integrationtest/
21
27. CONTINUOUS INTEGRATION
Unit Tests run at each
commit
Integration Tests run
“regularly”
• Daily
• Hourly
• Depending on the context
https://leanpub.com/integrationtest/
27
28. Y U FRAGILE?
Usage of infrastructure
resources
• External
• Multiple
https://leanpub.com/integrationtest/
28
35. MANAGING THE GAP RISK
https://leanpub.com/integrationtest/
35
In-memory databases
are easy to setup
h2 is such a database
• Compatibility modes for
most widespread DB
• jdbc:h2:mem:test;
MODE=Oracle
36. WEB SERVICES:
COPING WITH FRAGILITY
https://leanpub.com/integrationtest/
36
Web Services as
infrastructure resources
• Hosted on-site
• Or outside
Different architectures
• REST(ful)
• SOAP
37. WEB SERVICES:
COPING WITH FRAGILITY
Use Fakes under your
control
https://leanpub.com/integrationtest/
37
38. FAKE RESTFUL WEB SERVICES
https://leanpub.com/integrationtest/
38
Spark
• Micro web framework
• A la Sinatra
• Very few lines of code
• Just serve static JSON files
42. SMARTBEAR SOAPUI
Has a GUI
Good documentation
Understands
• Authentication
• Headers
• Etc.
https://leanpub.com/integrationtest/
42
43. SOAPUI USAGE
Get WSDL
• Either online
• Or from a file
Create MockService
• Craft the adequate response
Run the service
Point the dependency to
localhost
https://leanpub.com/integrationtest/
43
44. CHALLENGES TO THE PREVIOUS SCENARIO
Craft the adequate response?
• More likely get one from the real WS
• And tweak it
Running in an automated way
• Save the project
• Get the SOAPUI jar
• Read the project and launch
46. FAKING WEB SERVICE IN REAL-LIFE
Use the same rules as for UT
• Keep validation simple
• Test one thing
Keep setup simple
• Don’t put complex logic
• OK to duplicate setup in each
test
• Up to a point
Author:I,rolfB
https://leanpub.com/integrationtest/
46
53. EXAMPLE: DATASOURCE CONFIGURATION
@Bean
public DataSource ds() {
JndiDataSourceLookup lookup = new JndiDataSourceLookup();
lookup.setResourceRef(true);
return lookup.getDataSource("jdbc/MyDS");
}
@Bean
public DataSource ds() {
org.apache.tomcat.jdbc.pool.DataSource ds
= new org.apache.tomcat.jdbc.pool.DataSource();
ds.setDriverClassName("org.h2.Driver");
ds.setUrl("jdbc:h2:~/test");
ds.setUsername("sa");
ds.setMaxActive(1);
return ds;
}
https://leanpub.com/integrationtest/
53
54. FRAGMENT STRUCTURE
Main fragment
• Repository
• Service
• etc.
Prod DB
fragment
Test DB fragment
https://leanpub.com/integrationtest/
54
55. TIPS
Prevent coupling
• Use top-level assembly instead
of fragment references
Pool exhaustion check
• Set the maximum number of
connections in the pool to 1
Compile-time safety
• Use JavaConfig
https://leanpub.com/integrationtest/
55
56. NOW, HOW TO TEST?
https://leanpub.com/integrationtest/
56
Spring Test
• Integration with
widespread testing
frameworks
57. SAMPLE TESTNG TEST WITH SPRING
@ContextConfiguration(
classes = { MainConfig.class, TestDataSourceConfig.class }
)
@RunWith(SpringJUnit4ClassRunner.class)
public class OrderIT {
@Autowired
private OrderService orderService;
@Test
public void should_do_this_and_that() {
orderService.order();
Assert.assertThat(...)
}
}
https://leanpub.com/integrationtest/
57
58. TESTING WITH THE DATABASE
Transactions
• Bound to business feature
• Implemented on Service
layer
• @Transactional
https://leanpub.com/integrationtest/
58
59. TRANSACTION MANAGEMENT TIP
When tests fail
• How to audit state?
• Spring rollbacks
transactions
@Commit
https://leanpub.com/integrationtest/
59
skinparam dpi 150
class A
package "System Under Test" {
class B
class C
}
package database <<Database>> {
class D
}
A .right.> B
B .right.> C
C .right.> D
hide empty members