Download - September 2010 - Arquillian

Transcript
Page 1: September 2010 - Arquillian

Real Java EE TestingReal Java EE Testingwith with ArquillianArquillian andand ShrinkWrapShrinkWrap

Aslak KnutsenAslak KnutsenSenior Software EngineerSenior Software EngineerJBoss, by Red HatJBoss, by Red Hat

Don't mock me.

Page 2: September 2010 - Arquillian

2 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Agenda

● Barriers to testing

● Rethinking integration testing

● ShrinkWrap

● Arquillian

● Demo, demo, demo

● Future

● Q & A

#arquillian

Page 3: September 2010 - Arquillian

3 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Unit tests vs integration tests

Unit

● Fine-grained

● Simple

● Single API call

● Fast, fast, fast

● Easily run in an IDE

Integration

● Coarse-grained

● Complex

● Component interactions

● Sloooooooow

● Run in an IDE? How?

Page 4: September 2010 - Arquillian

4 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Common integration testing challenges

● Start a container environment

● Run a build to create/deploy application archive

● Mock dependent components

● Configure application to use test data source(s)

● Deal with (lack of) classpath isolation

Page 5: September 2010 - Arquillian

5 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

The “testing bandgap” and compounding effort

Unit Tests Integration Tests Functional Tests

Functional complexity

Configuration complexity

Tho

ught

and

Effo

rt

Page 6: September 2010 - Arquillian

6 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

What if integration testing could be...

● as easy as writing a unit test

● run in the IDE, leveraging incremental builds

● limited to select components running in isolation● ...avoiding the “big bang” integration roadblock

Page 7: September 2010 - Arquillian

7 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Test in-container!Test in-container!Skip the Build!

Page 8: September 2010 - Arquillian

8 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

An in-container approach to integration testing

1. Start or connect to a container

2. Package and deploy test case* as archive

3. Run test in-container

4. Collect results

5. Undeploy test archive

Page 9: September 2010 - Arquillian

9 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

container

n. a process that manages a runtime environment and providesresources, a component model and a set of services

Page 10: September 2010 - Arquillian

10 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Containers and components

● Component● Follows standard programming model● Encapsulates business logic● Packaged in deployable archive

● Container● Loads and executes components ● Provides services and resources

Page 11: September 2010 - Arquillian

11 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

What's been from Java EE?missing

^

Page 12: September 2010 - Arquillian

12 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

for your testsfor your testsA component model

Page 13: September 2010 - Arquillian

13 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Arquillian's testing continuum

Unit Tests Integration Tests Functional Tests

Functional complexity

Configuration complexity

Tho

ught

and

Effo

rt

Reducing enterprise testing to child's play

Page 14: September 2010 - Arquillian

14 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

How do we get there?

Page 15: September 2010 - Arquillian

15 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Step 1: Liberate tests from the build!

● Adds overhead to development cycle

● Slows down test execution

● Uses coarse-grained classpath/package

● Keep it manageable!● Well-defined “unit”● Classpath control

Page 16: September 2010 - Arquillian

16 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

n. a fluent Java API for programmatic creation of archives likeJARs, WARs and EARs; developed by the JBoss Community

Project lead: Andrew Lee Rubinger

Page 17: September 2010 - Arquillian

17 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Benefits of ShrinkWrap

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

● Simple, fluent API

● Micro deployments

● Export and debugging

● Tooling views (future)

Page 18: September 2010 - Arquillian

18 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Fluent archive creation

final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "slsb.jar") .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: September 2010 - Arquillian

19 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Build, what build?

@Deploymentpublic static Archive<?> createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class);}

Page 20: September 2010 - Arquillian

20 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Skip the build!

@Deploymentpublic static Archive<?> createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addPackage(TemperatureConverter.class.getPackage()) .addManifestResource(EmptyAsset.INSTANCE, "beans.xml");}

Page 21: September 2010 - Arquillian

21 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Step 2: Gut the plumbing!

● Manage lifecycle of container

● Enrich test class

● Package test archive

● Deploy test archive

● Invoke tests

● Capture test results

● What's left?● Pure test logic

Page 22: September 2010 - Arquillian

22 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

n. a container-oriented testing framework 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

Project lead: Aslak Knutsen

Page 23: September 2010 - Arquillian

23 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Make integration testing a breeze!

Arquillian project mission

Page 24: September 2010 - Arquillian

24 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Prove it.

@RunWith(Arquillian.class)public class GreeterTestCase {

@Deployment public static Archive<?> createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addClasses(Greeter.class, GreeterBean.class); } @EJB private Greeter greeter; @Test public void shouldBeAbleToInvokeEJB() throws Exception { assertEquals("Hello, Earthlings", greeter.greet("Earthlings")); }}

Page 25: September 2010 - Arquillian

25 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Benefits of Arquillian

● Write less (test) code

● As much or as little “integration” as you need

● Looks like a unit test, but you're in a real environment!● Easily lookup component to test● No hesitation when you need a resource

● Run same test in multiple containers

● It's a learning environment

Page 26: September 2010 - Arquillian

26 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

How is a container selected?

● Explain...

Page 27: September 2010 - Arquillian

27 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Arquillian tactical unit

● Unit testing framework runner

● Deployable test archive

● Test enrichers

● Test run mode

● Target container

● Test framework integrations

Page 28: September 2010 - Arquillian

28 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Supported unit testing frameworks

JUnit TestNG>= 4.8.1 >= 5.12.1

Page 29: September 2010 - Arquillian

29 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Test archive

● Assembled using ShrinkWrap API

● Declared with static @Deployment method

● Bundles:● Class/component to test● Supporting classes● Configuration and resource files● Dependent libraries

Page 30: September 2010 - Arquillian

30 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Micro deployments

● Deploy components in isolation

● Test one functional unit at a time

● Don't need to wait for full application build/startup

● Incremental integration● Hand pick components & resources● No “big bang” integration

Page 31: September 2010 - Arquillian

31 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Test enrichment

● Injection● Fields & method arguments● @Inject, @Resource, @EJB, @PersistenceContext, ...● CDI producer methods in test class

● Context management● Request & Conversation Test method ● Session Test class● Application Test class

● More to come...

Page 32: September 2010 - Arquillian

32 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Test run modes

● In-container● Test bundled with @Deployment archive● Archive deployed to container● Test runs inside container alongside application code● Test invokes application code directly (same JVM)

● As client● @Deployment archive is test archive (unmodified)● Archive deployed to the container● Test runs in original test runner● Test interacts as a remote client (e.g., HTTP client)

Page 33: September 2010 - Arquillian

33 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Classifying the containers

● By mode

● By compliance

Page 34: September 2010 - Arquillian

34 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Container modes

● Embedded● Same JVM as test runner● Test protocol either local or remote● Lifecycle controlled by Arquillian

● Remote● Separate JVM from test runner● Arquillian connects to running container● Tests executed over remote protocol

● Managed● Remote with lifecycle management

Page 35: September 2010 - Arquillian

35 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Container compliance

● Java EE application server (JBoss AS, GlassFish, etc)

● Servlet container (Tomcat, Jetty)

● Managed bean container (Weld SE, Spring)

● OSGi

● Whatever else comes along...

Page 36: September 2010 - Arquillian

36 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Supported containers

● JBoss AS 5.0 & 5.1 – Managed and remote

● JBoss AS 6 – Managed, remote and embedded

● JBoss JCA – Embedded

● GlassFish 3 – Remote and embedded

● Weld – SE embedded and EE mock embedded

● OpenWebBeans – embedded

● OpenEJB 3.1 – embedded

● OSGi – embedded

● Tomcat 6, Jetty 6.1 and Jetty 7 – embedded

● More on the way...

Page 37: September 2010 - Arquillian

37 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Container SPI, 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 38: September 2010 - Arquillian

38 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Power tools: test framework integration

● Test frameworks are services too

● Extend component model for tests

● Examples:● JSFUnit*● Cobertura● HTTPUnit● DBUnit● Selenium

* available

Page 39: September 2010 - Arquillian

39 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Arquillian...

● is a container-oriented testing framework

● provides a component model for tests

● handles test infrastructure & plumbing

● ships with a set of container implementations

● provides a little bit of magic ;)

Page 40: September 2010 - Arquillian

40 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Arquillian invasion strategy

● 1.0.0.Alpha3 out!

● More containers

● More framework integrations● DBUnit, HTTPUnit, Selenium

● Cloud

● Performance extension

● Component coverage

● Package-time bytecode manipulation● Exception, callback, assertion injection

● Tooling

Page 41: September 2010 - Arquillian

41 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

We're in print!

Enterprise JavaBeans 3.1, Sixth EditionO’Reilly - Andrew Lee Rubinger, et al

http://community.jboss.org/groups/oreillyejb6th

LOOK INSIDE!

Page 42: September 2010 - Arquillian

42 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

Get involved!

● Participate with us!● Share ideas on the forums (or IRC)● Give us feedback on releases – still in alpha!

● Fork us!● http://github.com/arquillian

● Meet us!● #jbosstesting channel on irc.freenode.net

● Write for us!● Share your stories – Blog! Tweet!● Document how it works

Page 43: September 2010 - Arquillian

43 Real Java EE Testing: Arquillian and ShrinkWrap | Aslak Knutsen

The JBoss Testing Initiative

● Comprehensive testing tool “stack”

● Establish a testing culture in Java EE● #jbosstesting channel on irc.freenode.net

● Filling voids● ShrinkWrap – Programmatic archive creation● Arquillian – Managed integration testing● JSFUnit – Gray-box JSF testing● Placeebo – Mock Java EE API implementations● Descriptors● Unit testing framework enhancements?

Page 44: September 2010 - Arquillian

Q & AQ & A

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