0% found this document useful (0 votes)
66 views36 pages

Real Java EE Testing With And: Arquillian

This document discusses testing Java EE applications using Arquillian and ShrinkWrap. It introduces some common challenges with integration testing like dealing with classpath isolation and the need to build and deploy applications. Arquillian and ShrinkWrap aim to address these by allowing tests to run inside a container without needing to build first, and by providing a simple API for assembling test archives. The document outlines some key benefits like writing less test code, testing at different levels of integration, and getting a more realistic environment.

Uploaded by

cast_exl
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views36 pages

Real Java EE Testing With And: Arquillian

This document discusses testing Java EE applications using Arquillian and ShrinkWrap. It introduces some common challenges with integration testing like dealing with classpath isolation and the need to build and deploy applications. Arquillian and ShrinkWrap aim to address these by allowing tests to run inside a container without needing to build first, and by providing a simple API for assembling test archives. The document outlines some key benefits like writing less test code, testing at different levels of integration, and getting a more realistic environment.

Uploaded by

cast_exl
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Real Java EE Testing with

Arquillian and ShrinkWrap

Don't fake it!

Pete Muir
Principal Software Engineer
Red Hat, Inc.
Agenda #testrevolution

 Testing, what's the problem?


 Integration testing challenges
 A component model for tests
 ShrinkWrap
 Arquillian
 Case study

2 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Why don't we test?

3 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


4 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir
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

5 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Skip the Build!
Test in-container!

6 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


container

n. manages a runtime environment and provides resources,


a component model and a set of services

7 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


A component
model for tests

8 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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?

9 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Testing bandgap and compounding effort
Thought and effort

Functional complexity

Infrastructural complexity

Unit Tests Integration Tests Functional Tests

10 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Testing continuum with Arquillian
Thought and effort

Functional complexity

Infrastructural complexity

Unit Tests Integration Tests Functional Tests

11 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


In-container approach to integration testing

 Separate container process


 Test deployed as archive
 Test runs in-container
 Results collected remotely

12 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Weighing in-container testing

Pros Cons
 Rely on shared memory  Still sloooow
 Pass-by-reference  Container set up
 Don't need remote views
 Each test is fully isolated
 Environment is “truer”

13 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Don't tie your tests to a build!

 Extra, external setup


 Adds time to tests
 Coarse-grained packaging

14 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


n. a simple, Apache-licensed Java API for assembling archives
like JARs, WARs, EARs; developed by the JBoss Community

15 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Benefits of ShrinkWrap

 IDE incremental compilation


 Save and re-run
 Skip the build!
 Simple API
 Tooling views
 Export and debugging
 Micro-deployments

16 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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

17 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Architecture overview

18 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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

19 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


The mission of the Arquillian project is to...

Make integration testing a breeze!

20 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Prove it!
@RunWith(Arquillian.class)
public  class  GreeterTestCase  {
     @Deployment
     public  staOc  JavaArchive  createTestArchive()  {
           return  ShrinkWrap.create("test.jar",  JavaArchive.class)
                 .addClasses(Greeter.class,  GreeterBean.class);
     }
     
     @EJB  private  Greeter  greeter;
     
     @Test
     public  void  shouldBeAbleToInjectEJB()  throws  ExcepOon  {
           assertEquals("Hello,  Earthlings",  greeter.greet("Earthlings"));
     }
}

21 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Arquillian guts the plumbing

 Manages lifecycle of container


 start/stop OR
 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

22 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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!

23 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Test frameworks

JUnit TestNG
>= 4.6 >= 5.10

24 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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

25 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Build, what build?
@Deployment
public  staOc  JavaArchive  createTestArchive()  {
     return  ShrinkWrap.create("test.jar",  JavaArchive.class)
           .addClasses(Greeter.class,  GreeterBean.class);
}

26 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Skip the build!
@Deployment
public  staOc  JavaArchive  createTestArchive()  {
     return  ShrinkWrap.create("test.jar",  JavaArchive.class)
           .addPackage(TranslateController.class.getPackage())
           .addManifestResource(
                 new  ByteArrayAsset("<beans/>".getBytes()),
                 ArchivePaths.create("beans.xml"));
}

27 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Containers

 Mode
 Capability

28 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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

29 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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

30 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Not just for Java EE
public  interface  DeployableContainer  {

     void  setup(Context  context,  ConfiguraOon  configuraOon);

     void  start(Context  context)  throws  LifecycleExcepOon;

     ContainerMethodExecutor  deploy(Context  context,  Archive<?>  archive)  


                 throws  DeploymentExcepOon;

     void  undeploy(Context  context,  Archive<?>  archive)


                 throws  DeploymentExcepOon;

     void  stop(Context  context)  throws  LifecycleExcepOon;

31 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Test enrichment

 Injection
 Fields & method arguments
 @Inject, @Resource, @EJB, etc.
 Contexts

Request & Conversation -> Test method

Session -> Test class

Application -> Test class
 Interceptors & decorators

32 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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)

33 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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 ;)

34 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


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
 Placebo – Mock Java EE API implementations
 JSFUnit – Gray-box JSF testing
 Contribute to the unit testing frameworks?

35 Real Java EE Testing: Arquillian and ShrinkWrap | Pete Muir


Q&A

http://jboss.org/arquillian
http://jboss.org/shrinkwrap

You might also like