47
Throwing Complexity Over the Wall: Throwing Complexity Over the Wall: Rapid Development for Enterprise Java Rapid Development for Enterprise Java Dan Allen Dan Allen JBoss, by Red Hat JBoss, by Red Hat mojavelinux mojavelinux Andrew Rubinger Andrew Rubinger JBoss, by Red Hat JBoss, by Red Hat ALRubinger ALRubinger

Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

Embed Size (px)

DESCRIPTION

For many, development of enterprise Java has long been an arduous undertaking. We're of the opinion that application programmers should be free to focus on their business logic only. In this session, we'll cover: • What makes us most productive? • What tasks should we be programming; more importantly, what shouldn't we? • What is a component model, and what does it buy us? • How is this stuff usable in the real world? We'll discuss how testing relates to the features of the Java EE 6 stack. By the end, we'll have introduced a pair of simple and powerful frameworks that render the testing of real enterprise components as natural as calling "add" on a CS101 Calculator.java.

Citation preview

Page 1: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

Throwing Complexity Over the Wall:Throwing Complexity Over the Wall: Rapid Development for Enterprise JavaRapid Development for Enterprise Java

Dan AllenDan AllenJBoss, by Red HatJBoss, by Red Hat mojavelinuxmojavelinux

Andrew RubingerAndrew RubingerJBoss, by Red HatJBoss, by Red Hat ALRubingerALRubinger

Page 2: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

2

Agenda

● Enterprise software development challenges

● Component models as a solution

● Tools that help you develop & test with confidence● ShrinkWrap - Skip the build● Arquillian - Test in-container

● Demo, demo, demo

● Arquillian's interchangeable parts

● Q & A (or more demos)

#arquillian

Page 3: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

3

General categorization of software

● Core concerns

● Cross-cutting concerns

● Plumbing

Page 4: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

4

What should you code?

● Core concerns● Business logic● Domain-specific

● Why?● Nothing can do this for you● It's what you are paid to do● It's a good investment of time● You get to 100% done sooner

Page 5: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

5

What should you avoid?

● “Conceptual weight”● Cross-cutting concerns● Plumbing

● Unnecessary LOC● Write less = maintain less● Improve signal-to-noise ratio

Page 6: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

6

Components models as a solution

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

● Container● Host process for deployed applications● Provides services and a runtime for components● Gives you powerful mechanisms for free

Page 7: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

7

So what is Java EE, really?

A standards-based platform that allows us to write business logic as components

Page 8: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

8

What's been from Java EE?missing

^

Page 9: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

9

for your testsfor your testsA component model

Page 10: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

10

Unit tests vs integration tests

Unit

● Attributes● Fine-grained● Simple● Single API call

● Perception● Fast, fast, fast● Easily run in an IDE

Integration

● Attributes● Coarse-grained● Complex● Component interactions

● Perception● Sloooooooow● Run in an IDE? How?

Page 11: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

11

No tests #fail

Page 12: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

12

Common integration testing challenges

● Bootstrap 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

● URLs, host names and ports, oh my!

Page 13: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

13

The testing “bandgap”

Unit Tests Integration Tests System Tests

Functionality

Setup & configuration

Com

ple x

ity (

Men

tal E

ffort

)

Page 14: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

14

What if integration testing could be...?

● as easy as writing a unit test

● run in the IDE (incremental builds, debugging, etc)

● ramped up in phases

● portable

Page 15: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

15

An in-container approach to integration testing

1. Start or connect to a container

2. Package and deploy test case to container

3. Run test in-container

4. Capture and report results

5. Undeploy test archive

Bring your test to the runtime...

...instead of managing the runtime from your test.

Page 16: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

16

Reducing enterprise testing to child's play

Unit Tests Integration Tests System Tests

Functionality

Setup & configuration

Arquillian's test continuum

Com

ple x

ity (

Men

tal E

ffort

)

Page 17: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

17

How do we get there?

Page 18: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

18

Test in-container!Test in-container!Skip the build!

Page 19: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

19

Step 1: Liberate your tests from the build!

● Builds are laborious● Add overhead● Slow down test execution● Coarse-grained packaging

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

Page 20: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

20

n. a fluent API for creating archives such as JARs, WARs andEARs in Java

Project lead: Andrew Lee Rubinger

http://jboss.org/shrinkwrap

Page 21: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

21

Benefits of ShrinkWrap

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

● Simple, fluent API

● Container deployment adapters

● Micro deployments

● Export and debugging

Page 22: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

22

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 23: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

23

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 24: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

24

Build, what build?

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

Page 25: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

25

Skip the build!

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

Page 26: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

26

Step 2: Gut the plumbing!

● Start/connect to container

● Package & deploy archive

● Inject resources

● Test logic

● Cleanup resources

● Undeploy archive

● Stop/disconnect from container

Page 27: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

27

n. a container-oriented testing framework that enablesdevelopers to create portable integration tests for enterpriseapplications; manages the lifecycle of a container and enriches,deploys and runs tests against it

Project lead: Aslak Knutsen

http://jboss.org/arquillian

Page 28: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

28

Make integration testing a breeze!

Arquillian project mission

Page 29: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

29

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 30: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

30

Prove it.

@RunWith(Arquillian.class)public class GreeterTestCase {

@Deployment public static Archive<?> createDeployment() { return ShrinkWrap.create(JavaArchive.class).addClass(Greeter.class) .addManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } @Inject Greeter greeter; @Test public void shouldBeAbleToInvokeManagedBean() throws Exception { assertEquals("Hello, Earthlings", greeter.greet("Earthlings")); }}

Page 31: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

31

DEMO!

Page 32: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

32

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 33: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

33

Page 34: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

34

Supported unit testing frameworks

JUnit TestNG>= 4.6 >= 5.10

Page 35: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

35

Test run modes

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

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

Page 36: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

36

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 37: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

37

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 38: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

38

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 39: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

39

Power tools: test framework integration

● Test frameworks are services too!

● Extends test component model

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

* available

Page 40: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

40

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 41: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

41

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: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

42

Get involved!

● Download us!● ShrinkWrap - http://jboss.org/shrinkwap● Arquillian - http://jboss.org/arquillian

● Participate with us!● Community Space

● Fork us!

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

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

Page 43: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

43

Emotional Ike

Now available as a Hudson build plugin!http://github.com/arquillian/arquillian-extensions

Page 44: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

Q & A (or more demos)Q & A (or more demos)

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

Page 45: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

45

Page 46: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

46

Page 47: Throwing complexity over the wall: Rapid development for enterprise Java (JavaOne 2010)

47