devoxx uk 2013 test-driven development with javaee 7, arquillian and embedded containers
DESCRIPTION
In this session, we introduce the Java developer to the Arquillian framework, Gradle and the Enterprise container technologies in Java EE 7. As a lucrative bonus we will cover building EJB and Java EE 7 tests applications with Gradle, the latest and greatest build framework for the Java platform, which improves on Apache Maven. The Java EE specification full contains three different containers, namely the Web, the EJB and the CDI containers. All of them can be reached using the Arquillian Framework, and this means there is now one general testing framework, which developers should learn as part of their professional duties. The session will cover writing meaningful tests for CDI, EJB and JAX-RS. Along the way, we will introduce new features of EJB 3.2, CDI 1.1 and RESTful Services. GlassFish Embedded Container 4.0 will be demonstrated.TRANSCRIPT
![Page 1: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/1.jpg)
![Page 2: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/2.jpg)
Test Driven Development with Java EE 7, Arquillian and
Enterprise Containers
Peter Pilgrim Java Champion, Software Developer
Independent Contractor
@peter_pilgrim
![Page 3: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/3.jpg)
3
Biography
■ Completed Java Sybase course in 1998
■ Working as an Independent Contractor
■ Founded JAVAWUG 2004-2010
■ Blue-chip business and Banking: IT, Deutsche, Credit Suisse, UBS, Lloyds Banking
Biography
![Page 4: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/4.jpg)
The Java EE 7 Developer User Guide Written by Peter Pilgrim
Late Summer 2013
![Page 5: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/5.jpg)
Agile Software Development?
![Page 6: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/6.jpg)
Why do we test?
![Page 7: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/7.jpg)
Architectural Competencies
Performance and Efficiency
Stability and Robustness
Maintainability and Refactor-ability
![Page 8: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/8.jpg)
Tools of the Trade
§ Frameworks: JUnit, TestNG and XUnit; JBehave, Spock, ScalaTest
§ Integrated Development Environment and Selenium
![Page 9: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/9.jpg)
How do we test?
![Page 10: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/10.jpg)
The Test Driven Cycle
Write A Failing Test
Make The Test Pass
Refactor the Test
Refactor the Main Code
Ensure All Tests Pass
"Oh, East is East, and West is West, and never the twain shall meet.”, Rudyard Kipling, 1892
![Page 11: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/11.jpg)
Essentials of Block Testing
Assign Act Assert
![Page 12: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/12.jpg)
Traditional Java Enterprise Testing
Outside of the Container
Mock and Stub objects
Writing Tests around Deployment
![Page 13: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/13.jpg)
Java EE 7
![Page 14: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/14.jpg)
Java EE 7 Framework Updates
Interface Boundary Endpoints
JAX RS 2.0
JMS 2.0
Bean Validation 1.1
Management and Storage
EJB 3.2
CDI 1.1
JPA 2.1
Web and HTML Service Endpoints
Servlet 3.1
WebSocket 1.0
JSF 2.2
JSON 1.0
![Page 15: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/15.jpg)
Time to Change JavaEE Testing
![Page 16: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/16.jpg)
Open Source Integration Testing
Peter Muir, David Blewin and Aslak Knutsen and from Red Hat JBoss.
![Page 17: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/17.jpg)
Arquillian Test Framework
Portable In-Container Integration Testing
Deployment of Unit Test and Dependencies together
Extension of Existing Test Framework Support
![Page 18: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/18.jpg)
Shrink Wrap
An Aid to Better Test Enterprise Components
“What if we could declare, in Java, an object to represent that archive?”
Builder pattern for Virtual JAR file
![Page 19: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/19.jpg)
Context & Dependency Injection 1.1
Inject Beans in strongly typed manner
Contextual Scopes, Qualifiers and Providers
Life-cycle, Event Management and Event Listeners
![Page 20: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/20.jpg)
Gradle Dependencies I dependencies {
compile 'org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec:1.0.0.Alpha1'
compile 'org.jboss.spec.javax.ejb:jboss-ejb-api_3.2_spec:1.0.0.Alpha2'
compile 'javax.enterprise:cdi-api:1.1-PFD’
compile 'javax.validation:validation-api:1.1.0.CR3'
compile 'org.hibernate:hibernate-validator:5.0.0.CR4'
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Draft-14'
runtime 'org.glassfish.main.extras:glassfish-embedded-all:4.0-b81’
testCompile 'org.jboss.arquillian.junit:arquillian-junit-container:1.0.3.Final'
testCompile 'org.jboss.arquillian.container:arquillian-glassfish-embedded-3.1:1.0.0.Final-SNAPSHOT'
testCompile 'junit:junit:4.11'
}
![Page 21: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/21.jpg)
Gradle Dependencies II dependencies {
//...
compile 'javax:javaee-api:7.0-b81'
runtime 'javax:javaee-api:7.0-b81'
//...
testCompile 'junit:junit:4.11'
}
![Page 22: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/22.jpg)
Arquillian Test Structure I @RunWith(Arquillian.class)
public class BasicUserDetailRepositoryTest {
@Deployment
public static JavaArchive createDeployment() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
.addClasses(BasicUserDetailRepository.class,
UserDetailRepository.class, User.class)
.addAsManifestResource(
EmptyAsset.INSTANCE, "beans.xml");
return jar;
}
/* ... */
}
![Page 23: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/23.jpg)
Arquillian Test Structure II @RunWith(Arquillian.class)
public class BasicUserDetailRepositoryTest { /* ... */
@Inject
private UserDetailRepository userDetailRepo;
@Test
public void shouldCreateUserInRepo() {
User user = new User("frostj", "Jack", "Frost");
assertFalse( userDetailRepo.containsUser(user ));
userDetailRepo.createUser(user);
assertTrue( userDetailRepo.containsUser(user ));
}
}
![Page 24: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/24.jpg)
Demo: Arquillian
Context Dependency & Injection
![Page 25: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/25.jpg)
Enterprise Java Beans 3.2 Session Beans as Java Co-Located or Remote Service Endpoints
Transaction Support
Lifecycle Event Management, Interception and Containment
Endpoints Extended to Web Services, JAX-RS and/or WebSockets
![Page 26: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/26.jpg)
Demo: Arquillian
Enterprise Java Beans
![Page 27: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/27.jpg)
HTML5 Java WebSocket 1.0
WebSocket connections are peer full-duplex HTTP interactions over a session
Reduced payload and lower latency
Designed for asynchronous operations, performance and Efficiency
![Page 28: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/28.jpg)
Demo: Arquillian
Java WebSocket API 1.0
![Page 29: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/29.jpg)
Embeddable Containers
So-called “Container-less” Web Application
Deliver an Container Embedded in Application
You Have Control: Size, Framework and Libraries
![Page 30: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/30.jpg)
GlassFish Embedded Initialization public class AbstractEmbeddedRunner {
private int port;
private GlassFish glassfish;
public AbstractEmbeddedRunner init() throws Exception{
BootstrapProperties bootstrapProperties = new BootstrapProperties();
GlassFishRuntime glassfishRuntime =
GlassFishRuntime.bootstrap(bootstrapProperties);
GlassFishProperties glassfishProperties = new GlassFishProperties();
glassfishProperties.setPort("http-listener", port);
// glassfishProperties.setPort("https-listener", port+1);
glassfish = glassfishRuntime.newGlassFish(glassfishProperties);
return this; } /* ... */
}
![Page 31: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/31.jpg)
GlassFish Embedded Start and Stop public class AbstractEmbeddedRunner {
/* ... */
public AbstractEmbeddedRunner start() throws Exception{
glassfish.start();
return this;
}
public AbstractEmbeddedRunner stop() throws Exception{
glassfish.stop();
return this;
} /* ... */
}
![Page 32: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/32.jpg)
GlassFish Embedded Deploy WAR public class AbstractEmbeddedRunner {
/* ... */
public AbstractEmbeddedRunner deploy( String args[]) throws Exception{
Deployer deployer = glassfish.getDeployer();
for (String s: args) {
String application = deployer.deploy(new File(s));
System.out.printf("deploying "+application);
}
return this;
}
/* ... */
}
![Page 33: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/33.jpg)
Demo: GlassFish Embedded
GlassFish 4.0 Embedded Server
![Page 34: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/34.jpg)
Heavyweight vs. Lightweight
How on earth do you weigh a Java container of any type?
![Page 35: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/35.jpg)
Developer Summary
![Page 36: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/36.jpg)
What You Will Do Tomorrow?
CHANGE!
Why Mock?
Arquillian
Embedded Containers
Test Philosophy
![Page 37: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/37.jpg)
Resources
§ Arquillian Integration Server http://arquillian.org/
§ Java EE 7 Specification http://jcp.org/en/jsr/detail?id=342
§ Java Web Socket API 1.0 http://java.net/projects/websocket-spec/downloads
§ GlassFish 4.0 Promoted Builds https://blogs.oracle.com/theaquarium/entry/downloading_latest_glassfish_4_promoted
§ WebSocket Client http://www.websocket.org/echo.html
§ GlassFish Embedded Instructions http://embedded-glassfish.java.net/
§ Thanks!
![Page 38: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/38.jpg)
Creative Commons Attributions § Brett Lider, Folded Ruler, http://www.flickr.com/photos/brettlider/1575417754/sizes/o/in/
photostream/
§ Daquella manera, Crutches donation to Haitian brothers and sisters, http://www.flickr.com/photos/daquellamanera/4390506017/sizes/l/in/photostream/
§ Marcus Tschiae, Electric Power Framework perspective http://www.flickr.com/photos/tschiae/8417927326/sizes/h/
§ Falk Neumman, September 5, 2005, You’ve Got A Fast Car, http://www.flickr.com/photos/coreforce/5910961411/
§ Stuart Webster, London skies from Waterloo Bridge, http://www.flickr.com/photos/stuartwebster/4192629903/sizes/o/
![Page 39: Devoxx UK 2013 Test-Driven Development with JavaEE 7, Arquillian and Embedded Containers](https://reader036.vdocuments.site/reader036/viewer/2022081403/5555cd07d8b42aaf158b4b36/html5/thumbnails/39.jpg)