Slide 1

Slide 1 text

INTEGRATION TESTING FROM THE TRENCHES @NICOLAS_FRANKEL

Slide 2

Slide 2 text

ME, MYSELF AND I h0ps://leanpub.com/integraContest/ 2

Slide 3

Slide 3 text

FROM THE BOOK h0ps://leanpub.com/integraContest/ 3

Slide 4

Slide 4 text

GENERALITIES INTEGRATION TESTING FROM THE TRENCHES 4 h0ps://leanpub.com/integraContest/

Slide 5

Slide 5 text

TESTING? Ø Unit Tes?ng Ø Muta?on Tes?ng Ø GUI Tes?ng Ø Performance Tes?ng •  Load TesCng •  Stress TesCng •  Endurance TesCng Ø Security Tes?ng Ø etc. h0ps://leanpub.com/integraContest/ 5

Slide 6

Slide 6 text

UNIT VS. INTEGRATION TESTING Ø Unit Tes?ng • TesCng a unit in isolaCon Ø Integra?on Tes?ng • TesCng the collaboraCon of mulCple units h0ps://leanpub.com/integraContest/ 6

Slide 7

Slide 7 text

EXAMPLE Ø Let’s take a prototype car h0ps://leanpub.com/integraContest/ 7

Slide 8

Slide 8 text

UNIT TESTING Ø Akin to tes?ng each nut and bolt separately h0ps://leanpub.com/integraContest/ 8

Slide 9

Slide 9 text

INTEGRATION TESTING Ø Akin to going on a test drive h0ps://leanpub.com/integraContest/ 9

Slide 10

Slide 10 text

UNIT + INTEGRATION TESTING Ø Not exclusive but complementary • 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? h0ps://leanpub.com/integraContest/ 10

Slide 11

Slide 11 text

SYSTEM UNDER TEST Ø SUT is what get tested Ø Techniques from Unit Tes?ng can be re-used • Dependency InjecCon • Test doubles h0ps://leanpub.com/integraContest/ 11

Slide 12

Slide 12 text

REMINDER: TEST DOUBLES Ø Dummy Ø Mock Ø Stub Ø Spy Ø Fake h0ps://leanpub.com/integraContest/ 12

Slide 13

Slide 13 text

TESTING IS ABOUT ROI Ø The larger the SUT • The more fragile the test • The less maintainable the test • The less the ROI h0ps://leanpub.com/integraContest/ 13

Slide 14

Slide 14 text

TESTING IS ABOUT ROI Ø Tests have to be organized in a certain way • The bigger the SUT • The less the number of tests h0ps://leanpub.com/integraContest/ 14

Slide 15

Slide 15 text

TESTING IS ABOUT ROI Ø Integra?on Tes?ng • Test nominal cases h0ps://leanpub.com/integraContest/ 15

Slide 16

Slide 16 text

INTEGRATION TESTING ISSUES INTEGRATION TESTING FROM THE TRENCHES 16 h0ps://leanpub.com/integraContest/

Slide 17

Slide 17 text

INTEGRATION TESTS CHALLENGES Ø Slow Ø Fragile Ø Hard to diagnose h0ps://leanpub.com/integraContest/ 17

Slide 18

Slide 18 text

Y U SLOW? Ø Use infrastructure resources Ø Use container h0ps://leanpub.com/integraContest/ 18

Slide 19

Slide 19 text

COPING WITH SLOWNESS Ø Separate Integra?on Tests from Unit Tests h0ps://leanpub.com/integraContest/ 19

Slide 20

Slide 20 text

INTEGRATION TESTS ARE STILL SLOW Ø Separa?ng IT doesn’t make them faster Ø But errors can be uncovered faster • Fail fast • It will speed tesCng h0ps://leanpub.com/integraContest/ 20

Slide 21

Slide 21 text

HOW TO SEPARATE? Ø Depends on the build tool • Ant • Maven • Gradle • Something else? h0ps://leanpub.com/integraContest/ 21

Slide 22

Slide 22 text

REMINDER: MAVEN LIFECYCLE h0ps://leanpub.com/integraContest/ 22

Slide 23

Slide 23 text

MAVEN SUREFIRE PLUGIN Ø Bound to the test phase Ø Runs by default • *Test • Test* • *TestCase h0ps://leanpub.com/integraContest/ 23

Slide 24

Slide 24 text

MAVEN FAILSAFE PLUGIN Ø “Copy” of Surefire Ø Different defaults • *IT • IT* • *ITCase h0ps://leanpub.com/integraContest/ 24

Slide 25

Slide 25 text

MAVEN FAILSAFE PLUGIN Ø One goal per lifecycle phase • pre-integration-test • integration-test • post-integration-test • verify Ø Must be bound explicitly h0ps://leanpub.com/integraContest/ 25

Slide 26

Slide 26 text

POM SNIPPET maven-failsafe-plugin 2.17 integration-test integration-test integration-test verify verify verify h0ps://leanpub.com/integraContest/ 26

Slide 27

Slide 27 text

CONTINUOUS INTEGRATION Ø Unit Tests run at each commit Ø Integra?on Tests run “regularly” • Daily • Hourly • Depending on the context h0ps://leanpub.com/integraContest/ 27

Slide 28

Slide 28 text

Y U FRAGILE? Ø Usage of infrastructure resources • External • MulCple h0ps://leanpub.com/integraContest/ 28

Slide 29

Slide 29 text

INFRASTRUCTURE RESOURCES Ø File system Ø Time Ø Databases Ø Web Services Ø Mail servers Ø FTP Servers Ø etc. h0ps://leanpub.com/integraContest/ 29

Slide 30

Slide 30 text

TIME/FILE SYSTEM: COPING WITH FRAGILITY h0ps://leanpub.com/integraContest/ 30 Ø Use Dependency Injec?on Ø Use abstrac?ons • e.g. java.nio.file.Path instead of java.util.File

Slide 31

Slide 31 text

DATABASES: COPING WITH FRAGILITY h0ps://leanpub.com/integraContest/ 31 Ø Test the Service layer • Mock the repository Ø Test the Repository layer • Mock the database???

Slide 32

Slide 32 text

DATABASES: COPING WITH FRAGILITY Ø Use Fakes under your control h0ps://leanpub.com/integraContest/ 32

Slide 33

Slide 33 text

ORACLE DATABASE USE-CASE h0ps://leanpub.com/integraContest/ 33 Ø Use an in-memory data source and hope for the best Ø Use Oracle Express and hope for the best Ø Use a dedicated remote schema for each developer • And your DBAs will hate you

Slide 34

Slide 34 text

TRADE-OFF h0ps://leanpub.com/integraContest/ 34 Ø The closer to the “real” infrastructure, the more complex the setup

Slide 35

Slide 35 text

MANAGING THE GAP RISK h0ps://leanpub.com/integraContest/ 35 Ø In-memory databases are easy to setup Ø h2 is such a database • CompaCbility modes for most widespread DB •  jdbc:h2:mem:test; MODE=Oracle

Slide 36

Slide 36 text

WEB SERVICES: COPING WITH FRAGILITY h0ps://leanpub.com/integraContest/ 36 Ø Web Services as infrastructure resources • Hosted on-site • Or outside Ø Different architectures • REST(ful) • SOAP

Slide 37

Slide 37 text

WEB SERVICES: COPING WITH FRAGILITY Ø Use Fakes under your control h0ps://leanpub.com/integraContest/ 37

Slide 38

Slide 38 text

FAKE RESTFUL WEB SERVICES h0ps://leanpub.com/integraContest/ 38 Ø Spark • Micro web framework • A la Sinatra • Very few lines of code • Just serve staCc JSON files

Slide 39

Slide 39 text

SPARK SAMPLE import static spark.Spark.*; import spark.*; public class SparkSample{ public static void main(String[] args) { setPort(5678); get("/hello", (request, response) -> { return "Hello World!"; }); get("/users/:name", (request, response) -> { return "User: " + request.params(":name"); }); get("/private", (request, response) -> { response.status(401); return "Go Away!!!"; }); } } h0ps://leanpub.com/integraContest/ 39

Slide 40

Slide 40 text

FAKE SOAP WEB SERVICES h0ps://leanpub.com/integraContest/ 40 Ø Possible with Spark • But not efficient • Remember about ROI

Slide 41

Slide 41 text

SMARTBEAR SOAPUI h0ps://leanpub.com/integraContest/ 41

Slide 42

Slide 42 text

SMARTBEAR SOAPUI Ø The framework to test SOAP WS • Has a GUI • Good documentaCon • Understands •  AuthenCcaCon •  Headers •  Etc. h0ps://leanpub.com/integraContest/ 42

Slide 43

Slide 43 text

SOAPUI USAGE Ø Get WSDL •  Either online •  Or from a file Ø Create MockService •  Cra\ the adequate response Ø Run the service Ø Point the dependency to localhost h0ps://leanpub.com/integraContest/ 43

Slide 44

Slide 44 text

CHALLENGES TO THE PREVIOUS SCENARIO Ø Craj 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

Slide 45

Slide 45 text

SOAPUI API WsdlProject project = new WsdlProject(); String wsdlFile = "file:src/test/resources/ip2geo.wsdl"; WsdlInterface wsdlInterface = importWsdl(project, wsdlFile, true)[0]; WsdlMockService fakeService = project.addNewMockService("fakeService"); WsdlOperation wsdlOp = wsdlInterface.getOperationByName("ResolveIP"); MockOperation fakeOp = fakeService.addNewMockOperation(wsdlOp); MockResponse fakeResponse = fakeOp.addNewMockResponse("fakeResponse"); fakeResponse.setResponseContent( ""); runner = fakeService.start(); h0ps://leanpub.com/integraContest/ 45

Slide 46

Slide 46 text

FAKING WEB SERVICE IN REAL-LIFE Ø Use the same rules as for UT • Keep validaCon 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, rolf B h0ps://leanpub.com/integraContest/ 46

Slide 47

Slide 47 text

IN-CONTAINER TESTING INTEGRATION TESTING FROM THE TRENCHES 47 h0ps://leanpub.com/integraContest/

Slide 48

Slide 48 text

INTEGRATION TESTING h0ps://leanpub.com/integraContest/ 48 Ø Collabora?on between classes Ø Boundaries with external dependencies Ø What did we forget?

Slide 49

Slide 49 text

DEPENDENCIES h0ps://leanpub.com/integraContest/ 49 Ø The most important dependency is the container! • Spring • Java EE • ApplicaCon server

Slide 50

Slide 50 text

SPRING CONFIGURATION TESTING h0ps://leanpub.com/integraContest/ 50 Ø So far, we can test: • Beans whose dependencies can be mocked • Beans that depend on fake resources Ø What about the configura?on?

Slide 51

Slide 51 text

DESIGNING TESTABLE CONFIGURATION h0ps://leanpub.com/integraContest/ 51 Ø Configura?on cannot be monolithic • Break down into fragments • Each fragment contains a set of either •  Real beans •  Fake beans

Slide 52

Slide 52 text

EXAMPLE: DATASOURCE CONFIGURATION h0ps://leanpub.com/integraContest/ 52 Ø Produc?on JNDI fragment Ø Test in- memory fragment

Slide 53

Slide 53 text

EXAMPLE: DATASOURCE CONFIGURATION h0ps://leanpub.com/integraContest/ 53

Slide 54

Slide 54 text

FRAGMENT STRUCTURE Ø  Main fragment • Repository • Service • etc. Ø  Prod DB fragment Ø  Test DB fragment h0ps://leanpub.com/integraContest/ 54

Slide 55

Slide 55 text

TIPS Ø Prevent coupling • Use top-level assembly instead of fragment references Ø Pool exhaus?on check • Set the maximum number of connecCons in the pool to 1 Ø Compile-?me safety • Use JavaConfig h0ps://leanpub.com/integraContest/ 55

Slide 56

Slide 56 text

NOW, HOW TO TEST? h0ps://leanpub.com/integraContest/ 56 Ø Spring Test • IntegraCon with widespread tesCng frameworks

Slide 57

Slide 57 text

SPRING TESTNG INTEGRATION h0ps://leanpub.com/integraContest/ 57 Ø AbstractTestNGSpringContextT ests •  AbstractTransacConalTestNGSprin gContextTests Ø Configurable context fragments •  @ContextConfiguraCon Ø Inject any bean in the test class

Slide 58

Slide 58 text

SAMPLE TESTNG TEST WITH SPRING @ContextConfiguration( classes = { MainCfg.class, AnotherCfg.class } ) public class OrderIT extends AbstractTestNGSpringContextTests { @Autowired private OrderService orderService; @Test public void should_do_this_and_that() { orderService.order(); Assert.assertThat(...) } } h0ps://leanpub.com/integraContest/ 58

Slide 59

Slide 59 text

TESTING WITH THE DATABASE Ø Transac?ons • Bound to business feature • Implemented on Service layer • @Transactional h0ps://leanpub.com/integraContest/ 59

Slide 60

Slide 60 text

TRANSACTION MANAGEMENT TIP Ø When tests fail • How to audit state? • Spring rollbacks transacCons Ø @Transac?onConfigura? on • defaultRollback = false h0ps://leanpub.com/integraContest/ 60

Slide 61

Slide 61 text

TRANSACTION MANAGEMENT SAMPLE @ContextConfiguration @TransactionConfiguration(defaultRollback = false) public class OverrideDefaultRollbackSpringTest
 extends AbstractTransactionalTestNGSpringContextTests { @Test @Rollback(true) public void transaction_will_be_rollbacked() { ... } @Test public void transaction_wont_be_rollbacked() { ... } } h0ps://leanpub.com/integraContest/ 61

Slide 62

Slide 62 text

THE JAVA EE WORLD Ø Java EE has unique challenges • CDI has no explicit wiring • Different applicaCon servers h0ps://leanpub.com/integraContest/ 62

Slide 63

Slide 63 text

DEPLOY ONLY WHAT YOU WANT Ø Standalone API to deploy only resources relevant to the test • Just pick and choose Ø Maven Integra?on • Gradle too… h0ps://leanpub.com/integraContest/ 63

Slide 64

Slide 64 text

SHRINKWRAP SAMPLE String srcMainWebapp = "src/main/webapp/"; ShrinkWrap.create(WebArchive.class, "myWar.war") .addClass(MyService.class) .addPackage(MyModel.class.getPackage()) .addAsWebInfResource("persistence.xml", "classes/META-INF/persistence.xml") .addAsWebInfResource( new File(srcMainWebapp, "WEB-INF/page/my.jsp"), "page/my.jsp") .addAsWebResource( new File(srcMainWebapp, "script/my.js"), "script/my.js") .setWebXML("web.xml"); h0ps://leanpub.com/integraContest/ 64

Slide 65

Slide 65 text

MAVEN INTEGRATION SAMPLE File[] libs = Maven.resolver() .loadPomFromFile("pom.xml") .importDependencies(COMPILE, RUNTIME) .resolve() .withTransitivity().asFile(); ShrinkWrap.create(WebArchive.class, 
 "myWar.war”)
 .addAsLibraries(libs); h0ps://leanpub.com/integraContest/ 65

Slide 66

Slide 66 text

DIFFERENT APPLICATION SERVERS Ø Abstrac?on layer to •  Download •  Deploy applicaCons •  Test Ø Container adapters •  TomEE •  JBoss •  Weld •  etc. Ø Full Maven integra?on h0ps://leanpub.com/integraContest/ 66

Slide 67

Slide 67 text

ARQUILLIAN TEST SAMPLE public class ArquillianSampleIT extends Arquillian { @Inject private MyService myService; @Deployment public static JavaArchive createDeployment() { return ...; } @Test public void should_handle_service() { Object value = myService.handle(); Assert.assertThat(...); } } h0ps://leanpub.com/integraContest/ 67

Slide 68

Slide 68 text

ARQUILLIAN CONFIGURATION SAMPLE -1 -1 h0ps://leanpub.com/integraContest/ 68

Slide 69

Slide 69 text

Q&A h0ps://leanpub.com/integraContest/ 69 hpps://goo.gl/mXv7ZE @nicolas_frankel hpp://frankel.in/