Testing Java EE applications Karel Piwko JBoss WFK QA November 2010 Testing Java EE applications | Karel Piwko2 Outline Testing applications  Why do we test applications?  How do we test applications? Testing Java EE applications  Problems  Useful tools  Testing Java EE the JBoss way Testing Java EE applications | Karel Piwko3 Why do we test applications?  Developers tend to see their application often as a perfect piece of code Testing Java EE applications | Karel Piwko4 Why do we test applications?  But often... Testing Java EE applications | Karel Piwko5 Why do we test applications?  Last fix was a two-liner... Testing Java EE applications | Karel Piwko6 Why do we test applications?  Ensure the software contains the least bugs possible  Verification vs. validation  complies with specifications and conditions specified in a development phase  accomplishes expected requirements  Sooner means cheaper Testing Java EE applications | Karel Piwko7 How do we test applications?  Test approach  white box testing  black box testing  gray box testing  Test type  code analysis  unit test  integration test  functional test  system test Testing Java EE applications | Karel Piwko8 White box testing  Tests internal structure of the application  branching, control flow, data flow  Usually unit level  Drawback  can't test code which is not written Testing Java EE applications | Karel Piwko9 White box testing Testing Java EE applications | Karel Piwko10 Black box testing  Internal structure of the application not known or not required  Specification and requirements are used to validate functional behavior  Usually integration or functional level  Drawback  results can be influenced by state of the black-box component Testing Java EE applications | Karel Piwko11 Black box testing GET /index.jsp black-box ... Does application welcome page contain our logo? yes /no Testing Java EE applications | Karel Piwko12 Code analysis  Code verification  Static analysis  type analysis, bug pattern searching  Dynamic analysis  code coverage  debuggers, profilers  Formal methods  based on mathematical theories  full automation, soundness, completeness, termination Testing Java EE applications | Karel Piwko13 Code coverage  Determine how much of the code is tested => use the information to add test cases  Tool: EMMA, Cobertura  branch, live, method, class, package coverage reports  Unit versus integration tests  generally the possible coverage result will decrease with test level  test coverage results can be misleading if we sum different levels Testing Java EE applications | Karel Piwko14 Unit tests  Tests individual units of source code in isolation  enforces code style  stubs and mock objects  Usually created by programmers  test driven development possible  Can run in an IDE  The granularity of unit matters  It is difficult to cover all execution paths of the application Testing Java EE applications | Karel Piwko15 Unit test granularity We coupled two units together! Testing Java EE applications | Karel Piwko16 Unit test granularity  Solution  Decouple contracts and its implementation (constructor)  Provide better interface for Quest We cannot easily find out what happened inside! Testing Java EE applications | Karel Piwko17 Integration and functional tests  Tests groups of verified units together  Complex  Cannot be easily run in an IDE  Continuous integration testing  run unit and integration tests after each modification  version control system (SVN, Git, Hq, ...)  automation of the process (Hudson) => feature and nightly builds Testing Java EE applications | Karel Piwko18 System tests  Compliance of the system to its specified requirements  Smoke tests  Verification of the system before performance tests  Load tests  Behavior under load  Stress tests  Behavior under load beyond usual expectations  Soak tests  Behavior with a long period of the time Testing Java EE applications | Karel Piwko19 Testing Java EE applications  Problems  Java EE applications are complex, thus it is difficult to isolate components  application server (JBoss AS, GlassFish, WebSphere, ...)  communication (JMS, HornetQ, ...)  UI (web based - JSF, JSP, RichFaces, ...)  database layer (JPA, Hibernate, ...)  ...  Testing is highly time consuming, not enjoyable and hard to be done properly => Leads to even more stubbing, mocking and innovative approaches Testing Java EE applications | Karel Piwko20 What do we need to test Java EE applications?  Build tool  Maven, Ant, Ivy, Gradle  Test framework  TestNG, JUnit  Mock framework  Mockito, jMock, JMockit, EasyMock  UI testing frameworks  Selenium, WebDriver, JSFUnit, Ajocado, HTMLUnit ... and lot of others Testing Java EE applications | Karel Piwko21 Testing Java EE the JBoss way  Goal  make active mocks easier to use  configure applications to use test data sources  deal with classpath isolation in container  create/deploy application archive  handle “too many frameworks involved” problem => give developers tools to make Java EE testing fun again Testing Java EE applications | Karel Piwko22 ShrinkWrap  Simple API to assemble archives like JARs, WARs and EARs  allows building integration bits directly in the code  keeps the isolation in test execution  Used by Arquillian internally http://community.jboss.org/wiki/Shrinkwrap Skip the Build! Testing Java EE applications | Karel Piwko23 ShrinkWrap  How to build WAR in application?  Many other ways how to include files in an Archive  by package, class name, file, stream, zip Testing Java EE applications | Karel Piwko24 ShrinkWrap extensions  ShrinkWrap dependencies  Resolves dependencies from Maven repositories  Can reuse information in POM file to reduce verbosity Testing Java EE applications | Karel Piwko25 Arquillian  Brings you the way to write integration tests in a same way as you do for unit tests  manages lifecycle of a container  bundles and deploys test archive  enriches test classes  captures test results and  Does not bind a build to the test, configuration is kept externally Arquillian makes integration testing a breeze! Testing Java EE applications | Karel Piwko26 Arquillian  Can be used within multiple build tools, containers and test frameworks, specialized on EE testing  Expendable via SPI interface Testing Java EE applications | Karel Piwko27 Arquillian and @Inject (In-container testing) Testing Java EE applications | Karel Piwko28 Arquillian and @EJB (In-container testing) Testing Java EE applications | Karel Piwko29 Arquillian As-Client Testing (Out of container) Testing Java EE applications | Karel Piwko30 Questions? kpiwko@redhat.com | www.redhat.com