Your SlideShare is downloading. ×
0
Real Java EE Testing with
Arquillian and ShrinkWrap


                            Don't fake it!




Dan Allen
Senior Soft...
Who am I?

    ●   Author of Seam in Action, Manning 2008
    ●   Seam Community Liaison
    ●   Weld, Seam & Arquillian p...
Agenda                                                           #testrevolution

    ●   Testing, what's the problem?
   ...
Why don't we test?




4   Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
5   Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
Common integration testing challenges

    ●   Active mocks to stand in for collaborators
    ●   Configure application to...
Skip the Build!
                                                         Test in-container!




7   Real Java EE Testing: ...
container

    n. manages a runtime environment and provides resources,
    a component model and a set of services




8 ...
A component
                                                         model for tests




9   Real Java EE Testing: Arquill...
Unit tests vs integration tests

 Unit                                                 Integration
     ●   Fine-grained  ...
Testing bandgap and compounding effort




                                                                               ...
Testing continuum with Arquillian




                                                                                    ...
In-container approach to integration testing

     ●   Separate container process
     ●   Test deployed as archive
     ●...
Weighing in-container testing

 Pros                                              Cons
     ●   Rely on shared memory     ...
Don't tie your tests to a build!

     ●   Extra, external setup
     ●   Adds time to tests
     ●   Coarse-grained packa...
n. a simple, Apache-licensed Java API for assembling archives
     like JARs, WARs, EARs; developed by the JBoss Community...
Benefits of ShrinkWrap

     ●   IDE incremental compilation
          ●   Save and re-run
          ●   Skip the build!
 ...
Fluent archive creation
     final JavaArchive archive = ShrinkWrap.create("slsb.jar", JavaArchive.class)
         .addCla...
Architecture overview




19          Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
n. a simple, Apache-licensed test harness that abstracts away
     container lifecycle and deployment from test logic so d...
The mission of the Arquillian project is to...




          Make integration testing a breeze!




21           Real Java...
Prove it!
     @RunWith(Arquillian.class)
     public class GreeterTestCase {
      @Deployment
      public static JavaAr...
Arquillian guts the plumbing

     ●   Manages lifecycle of container
          ●   start/stop
          ●   bind/unbind
 ...
Benefits of Arquillian

     ●   Write less (test) code
     ●   As much or as little “integration” as you need
     ●   L...
Test frameworks




           JUnit                                     TestNG
              >= 4.6                      ...
Test archives

     ●   Built using ShrinkWrap API
     ●   Bundle:
          ●   code under test
          ●   dependent ...
Build, what build?
     @Deployment
     public static JavaArchive createTestArchive() {
       return ShrinkWrap.create("...
Skip the build!
     @Deployment
     public static JavaArchive createTestArchive() {
       return ShrinkWrap.create("tes...
Micro deployments

     ●   Deploy components in isolation
     ●   Test one slice at a time
     ●   Don't need to wait f...
Containers

     ●   Mode
     ●   Capability




30                    Real Java EE Testing: Arquillian and ShrinkWrap | ...
Container modes

     ●   Embedded
         ●   Same JVM as test runner
         ●   Tests executed by native test runner
...
Container capabilities

     ●   Java EE application server (JBoss AS, GlassFish, etc)
     ●   Servlet container (Tomcat,...
Not just for Java EE
     public interface DeployableContainer {

         void setup(Context context, Configuration confi...
Test enrichment

     ●   Injection
          ●   Fields & method arguments
          ●   @Inject, @Resource, @EJB, etc.
 ...
Test run modes

     ●   In-container
          ●   Test bundled in @Deployment archive
          ●   Archive deployed to ...
In-container testing MDBs: A case study

     ●   Asynchronous
          ●   How will client know server is done processin...
Arquillian is...

     ●   an innovative approach to testing
     ●   a component model for tests
     ●   test infrastruc...
JBoss Testing Initiative

     ●   Comprehensive testing tool “stack”
     ●   Establish a testing culture in Java EE
    ...
Get Involved!

     ●   Active and open community
     ●   How to contribute:
          ●   Ideas on forums or IRC
       ...
http://jboss.org/arquillian




40                 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
Q&A                  http://jboss.org/arquillian
                     http://jboss.org/shrinkwrap




Dan Allen
Senior Sof...
Upcoming SlideShare
Loading in...5
×

Real Java EE Testing with Arquillian and ShrinkWrap

10,581

Published on

Recorded on 2010-04-30 at the Northern Virginia Software Symposium, a stop on the NFJS 2010 tour, this presentation introduces Arquillian, an extension for TestNG and JUnit that provides a component model for tests, making it simple to test real components inside a real container.

Published in: Technology

Transcript of "Real Java EE Testing with Arquillian and ShrinkWrap"

  1. 1. Real Java EE Testing with Arquillian and ShrinkWrap Don't fake it! Dan Allen Senior Software Engineer JBoss, by Red Hat
  2. 2. Who am I? ● Author of Seam in Action, Manning 2008 ● Seam Community Liaison ● Weld, Seam & Arquillian project member ● JSR-314 (JSF 2) EG representative ● Open Source advocate mojavelinux 2 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  3. 3. Agenda #testrevolution ● Testing, what's the problem? ● Integration testing challenges ● A component model for tests ● ShrinkWrap ● Arquillian ● Case study 3 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  4. 4. Why don't we test? 4 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  5. 5. 5 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  6. 6. Common integration testing challenges ● Active mocks to stand in for collaborators ● Configure application to use test data source(s) ● Deal with (lack of) classpath isolation ● Run a build to create/deploy application archive 6 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  7. 7. Skip the Build! Test in-container! 7 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  8. 8. container n. manages a runtime environment and provides resources, a component model and a set of services 8 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  9. 9. A component model for tests 9 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  10. 10. Unit tests vs integration tests Unit Integration ● Fine-grained ● Coarse-grained ● Simple ● Complex ● Test single API call ● Test intricate web of calls ● Fast, fast, fast ● Sloooooooow ● Easily run in an IDE ● Run in an IDE? How? 10 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  11. 11. Testing bandgap and compounding effort Functional complexity Thought and effort Unit Tests Integration Tests Functional Tests 11 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  12. 12. Testing continuum with Arquillian Functional complexity Thought and effort Unit Tests Integration Tests Functional Tests 12 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  13. 13. In-container approach to integration testing ● Separate container process ● Test deployed as archive ● Test runs in-container ● Results collected remotely 13 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  14. 14. Weighing in-container testing Pros Cons ● Rely on shared memory ● Lack of isolation ● Pass-by-reference ● Environment not “true” ● Don't need remote views ● Managed concurrency 14 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  15. 15. Don't tie your tests to a build! ● Extra, external setup ● Adds time to tests ● Coarse-grained packaging 15 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  16. 16. n. a simple, Apache-licensed Java API for assembling archives like JARs, WARs, EARs; developed by the JBoss Community 16 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  17. 17. Benefits of ShrinkWrap ● IDE incremental compilation ● Save and re-run ● Skip the build! ● Simple API ● Tooling views ● Export and debugging ● Micro-deployments 17 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  18. 18. Fluent archive creation final JavaArchive archive = ShrinkWrap.create("slsb.jar", JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class); System.out.println(archive.toString(true)); Yields output: slsb.jar: /com/ /com/acme/ /com/acme/app/ /com/acme/app/ejb3/ /com/acme/app/ejb3/Greeter.class /com/acme/app/ejb3/GreeterBean.class 18 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  19. 19. Architecture overview 19 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  20. 20. n. a simple, Apache-licensed test harness that abstracts away container lifecycle and deployment from test logic so developers can easily develop a broad range of integration tests for their enterprise Java applications; developed by the JBoss Community 20 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  21. 21. The mission of the Arquillian project is to... Make integration testing a breeze! 21 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  22. 22. Prove it! @RunWith(Arquillian.class) public class GreeterTestCase { @Deployment public static JavaArchive createTestArchive() { return ShrinkWrap.create("test.jar", JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class); } @EJB private Greeter greeter; @Test public void shouldBeAbleToInjectEJB() throws Exception { assertEquals("Hello, Earthlings", greeter.greet("Earthlings")); } } 22 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  23. 23. Arquillian guts the plumbing ● Manages lifecycle of container ● start/stop ● bind/unbind ● Enriches test class (e.g, @Inject, @EJB, @Resource) ● Bundles test archive ● code under test ● libraries ● test class and invoker (in-container run mode only) ● Negotiates deployment of test archive ● Captures test results and failures 23 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  24. 24. Benefits of Arquillian ● Write less (test) code ● As much or as little “integration” as you need ● Looks like a unit test, get fully functioning components ● Simple way to get an instance of component under test ● You don't hesitate when you need a resource ● Same test, multiple containers ● It's the real deal! 24 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  25. 25. Test frameworks JUnit TestNG >= 4.6 >= 5.10 25 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  26. 26. Test archives ● Built using ShrinkWrap API ● Bundle: ● code under test ● dependent Libraries ● test class and invoker (in-container run mode only) ● Deployed to container before test is executed ● Undeployed after test is executed 26 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  27. 27. Build, what build? @Deployment public static JavaArchive createTestArchive() { return ShrinkWrap.create("test.jar", JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class); } 27 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  28. 28. Skip the build! @Deployment public static JavaArchive createTestArchive() { return ShrinkWrap.create("test.jar", JavaArchive.class) .addPackage(TranslateController.class.getPackage()) .addManifestResource( new ByteArrayAsset("<beans/>".getBytes()), ArchivePaths.create("beans.xml")); } 28 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  29. 29. Micro deployments ● Deploy components in isolation ● Test one slice at a time ● Don't need to wait for full application build/startup ● Tune size of integration ● Layers by inclusion ● No “big bang” 29 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  30. 30. Containers ● Mode ● Capability 30 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  31. 31. Container modes ● Embedded ● Same JVM as test runner ● Tests executed by native test runner ● Lifecycle controlled by Arquillian ● Remote ● Separate JVM from test runner ● Tests executed over remote protocol ● Arquillian likely binds to ports 31 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  32. 32. Container capabilities ● Java EE application server (JBoss AS, GlassFish, etc) ● Servlet container (Tomcat, Jetty) ● Managed bean container (Weld SE, Spring) ● OSGi ● SPI allows you to introduce any other container 32 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  33. 33. Not just for Java EE public interface DeployableContainer { void setup(Context context, Configuration configuration); void start(Context context) throws LifecycleException; ContainerMethodExecutor deploy(Context context, Archive<?> archive) throws DeploymentException; void undeploy(Context context, Archive<?> archive) throws DeploymentException; void stop(Context context) throws LifecycleException; } 33 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  34. 34. Test enrichment ● Injection ● Fields & method arguments ● @Inject, @Resource, @EJB, etc. ● Contexts ● Request & Conversation  Test method ● Session  Test class ● Application  Test class ● Interceptors & decorators 34 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  35. 35. Test run modes ● In-container ● Test bundled in @Deployment archive ● Archive deployed to container ● Test runs inside container with code under test ● Test invokes code under test directly (same JVM) ● Local ● @Deployment archive unmodified ● Archive deployed to the container ● Test runs in original test runner ● Test interacts as a remote client (e.g., HTTP client) 35 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  36. 36. In-container testing MDBs: A case study ● Asynchronous ● How will client know server is done processing? ● Thread.sleep() is prone to transient failures ● No return value ● How do we check post-conditions? ● In-container == same JVM ● Gives test control ● Allows test to inspect contexts 36 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  37. 37. Arquillian is... ● an innovative approach to testing ● a component model for tests ● test infrastructure & plumbing ● a set of container implementations ● a little bit of magic ;) 37 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  38. 38. JBoss Testing Initiative ● Comprehensive testing tool “stack” ● Establish a testing culture in Java EE ● #jbosstesting on irc.freenode.net ● Filling voids ● ShrinkWrap – Programmatic archive creation ● Arquillian – Managed integration testing ● Placeebo – Mock Java EE API implementations ● JSFUnit – Gray-box JSF testing ● Contribute to the unit testing frameworks? 38 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  39. 39. Get Involved! ● Active and open community ● How to contribute: ● Ideas on forums or IRC ● Feedback on releases – still in alpha! ● Enhancements and bug fixes – we love patches! ● Documentation ● Blogs – share your stories! ● Come meet us: ● http://jboss.org/arquillian ● #jbosstesting on irc.freenode.net 39 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  40. 40. http://jboss.org/arquillian 40 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen
  41. 41. Q&A http://jboss.org/arquillian http://jboss.org/shrinkwrap Dan Allen Senior Software Engineer JBoss, by Red Hat
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×