real java ee testing with arquillian and shrinkwrap

41
Real Java EE Testing with Real Java EE Testing with Arquillian Arquillian and and ShrinkWrap ShrinkWrap Dan Allen Dan Allen Senior Software Engineer Senior Software Engineer JBoss, by Red Hat JBoss, by Red Hat Don't fake it!

Upload: dan-allen

Post on 19-May-2015

10.423 views

Category:

Technology


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Real Java EE Testing with Arquillian and ShrinkWrap

Real Java EE Testing withReal Java EE Testing with

ArquillianArquillian andand ShrinkWrapShrinkWrap

Dan AllenDan AllenSenior Software EngineerSenior Software EngineerJBoss, by Red HatJBoss, by Red Hat

Don't fake it!

Page 2: Real Java EE Testing with Arquillian and ShrinkWrap

2 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 3: Real Java EE Testing with Arquillian and ShrinkWrap

3 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Agenda

● Testing, what's the problem?

● Integration testing challenges

● A component model for tests

● ShrinkWrap

● Arquillian

● Case study

#testrevolution

Page 4: Real Java EE Testing with Arquillian and ShrinkWrap

4 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Why don't we test?

Page 5: Real Java EE Testing with Arquillian and ShrinkWrap

5 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Page 6: Real Java EE Testing with Arquillian and ShrinkWrap

6 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 7: Real Java EE Testing with Arquillian and ShrinkWrap

7 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Test in-container!Test in-container!

Skip the Build!

Page 8: Real Java EE Testing with Arquillian and ShrinkWrap

8 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

container

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

Page 9: Real Java EE Testing with Arquillian and ShrinkWrap

9 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

model for testsmodel for tests

A component

Page 10: Real Java EE Testing with Arquillian and ShrinkWrap

10 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Unit tests vs integration tests

Unit

● Fine-grained

● Simple

● Test single API call

● Fast, fast, fast

● Easily run in an IDE

Integration

● Coarse-grained

● Complex

● Test intricate web of calls

● Sloooooooow

● Run in an IDE? How?

Page 11: Real Java EE Testing with Arquillian and ShrinkWrap

11 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Testing bandgap and compounding effort

Unit Tests Integration Tests Functional Tests

Functional complexity

Thought and effort

Page 12: Real Java EE Testing with Arquillian and ShrinkWrap

12 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Testing continuum with Arquillian

Unit Tests Integration Tests Functional Tests

Functional complexity

Thought and effort

Page 13: Real Java EE Testing with Arquillian and ShrinkWrap

13 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

In-container approach to integration testing

● Separate container process

● Test deployed as archive

● Test runs in-container

● Results collected remotely

Page 14: Real Java EE Testing with Arquillian and ShrinkWrap

14 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Weighing in-container testing

Pros

● Rely on shared memory

● Pass-by-reference

● Don't need remote views

● Managed concurrency

Cons

● Lack of isolation

● Environment not “true”

Page 15: Real Java EE Testing with Arquillian and ShrinkWrap

15 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Don't tie your tests to a build!

● Extra, external setup

● Adds time to tests

● Coarse-grained packaging

Page 16: Real Java EE Testing with Arquillian and ShrinkWrap

16 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 17: Real Java EE Testing with Arquillian and ShrinkWrap

17 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Benefits of ShrinkWrap

● IDE incremental compilation● Save and re-run● Skip the build!

● Simple API

● Tooling views

● Export and debugging

● Micro-deployments

Page 18: Real Java EE Testing with Arquillian and ShrinkWrap

18 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Fluent archive creation

final JavaArchive archive = ShrinkWrap.create("slsb.jar", JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class);System.out.println(archive.toString(true));

slsb.jar:/com//com/acme//com/acme/app//com/acme/app/ejb3//com/acme/app/ejb3/Greeter.class/com/acme/app/ejb3/GreeterBean.class

Yields output:

Page 19: Real Java EE Testing with Arquillian and ShrinkWrap

19 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Architecture overview

Page 20: Real Java EE Testing with Arquillian and ShrinkWrap

20 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

n. a simple, Apache-licensed test harness that abstracts awaycontainer lifecycle and deployment from test logic so developerscan easily develop a broad range of integration tests for theirenterprise Java applications; developed by the JBoss Community

Page 21: Real Java EE Testing with Arquillian and ShrinkWrap

21 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Make integration testing a breeze!

The mission of the Arquillian project is to...

Page 22: Real Java EE Testing with Arquillian and ShrinkWrap

22 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 23: Real Java EE Testing with Arquillian and ShrinkWrap

23 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 24: Real Java EE Testing with Arquillian and ShrinkWrap

24 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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!

Page 25: Real Java EE Testing with Arquillian and ShrinkWrap

25 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Test frameworks

JUnit TestNG>= 4.6 >= 5.10

Page 26: Real Java EE Testing with Arquillian and ShrinkWrap

26 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 27: Real Java EE Testing with Arquillian and ShrinkWrap

27 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Build, what build?

@Deploymentpublic static JavaArchive createTestArchive() { return ShrinkWrap.create("test.jar", JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class);}

Page 28: Real Java EE Testing with Arquillian and ShrinkWrap

28 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Skip the build!

@Deploymentpublic static JavaArchive createTestArchive() { return ShrinkWrap.create("test.jar", JavaArchive.class) .addPackage(TranslateController.class.getPackage()) .addManifestResource( new ByteArrayAsset("<beans/>".getBytes()), ArchivePaths.create("beans.xml"));}

Page 29: Real Java EE Testing with Arquillian and ShrinkWrap

29 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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”

Page 30: Real Java EE Testing with Arquillian and ShrinkWrap

30 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Containers

● Mode

● Capability

Page 31: Real Java EE Testing with Arquillian and ShrinkWrap

31 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 32: Real Java EE Testing with Arquillian and ShrinkWrap

32 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 33: Real Java EE Testing with Arquillian and ShrinkWrap

33 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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;

}

Page 34: Real Java EE Testing with Arquillian and ShrinkWrap

34 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

Test enrichment

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

● Contexts● Request & Conversation Test method ● Session Test class● Application Test class

● Interceptors & decorators

Page 35: Real Java EE Testing with Arquillian and ShrinkWrap

35 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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)

Page 36: Real Java EE Testing with Arquillian and ShrinkWrap

36 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 37: Real Java EE Testing with Arquillian and ShrinkWrap

37 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 38: Real Java EE Testing with Arquillian and ShrinkWrap

38 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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?

Page 39: Real Java EE Testing with Arquillian and ShrinkWrap

39 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

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

Page 40: Real Java EE Testing with Arquillian and ShrinkWrap

40 Real Java EE Testing: Arquillian and ShrinkWrap | Dan Allen

http://jboss.org/arquillian

Page 41: Real Java EE Testing with Arquillian and ShrinkWrap

Q & AQ & A

Dan AllenDan AllenSenior Software EngineerSenior Software EngineerJBoss, by Red HatJBoss, by Red Hat

http://jboss.org/arquillianhttp://jboss.org/arquillianhttp://jboss.org/shrinkwraphttp://jboss.org/shrinkwrap