Test Java EE applications with Arquillian Ivan St. Ivanov.

Slides:



Advertisements
Similar presentations
Java EE - Introduction -
Advertisements

® IBM Software Group © 2010 IBM Corporation What’s New in Profiling & Code Coverage RAD V8 April 21, 2011 Kathy Chan
Test Automation Framework Ashesh Jain 2007EE50403 Manager Amit Maheshwari.
ISE 390 Dynamic Web Development Java EE Web Applications.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
An architecture for webb applications, J2EE
Model-View-Controller ("MVC") This is a architectural design pattern for interactive applications. This design pattern organizes an interactive application.
JBoss Seam: Contextual Components Jason Bechtel
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Eclipse Web Tools Platform.
Java 2 – Enterprise Edition Kevin J. LaFata April 21, 2003 UM – St. Louis.
Current Topics in Programming Languages Lecture 15_1 George Koutsogiannakis SUMMER
1.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.Public 1 Testing Java EE Applications using Arquillian Reza Rahman Java EE/GlassFish.
Session-01. What is a Servlet? Servlet can be described in many ways, depending on the context: 1.Servlet is a technology i.e. used to create web application.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
1 Lecture 18 George Koutsogiannakis/Spring 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
JSP and Servlets Lecture notes by Theodoros Anagnostopoulos.
WebOPI -Build & Setup Xihui Chen April, 2013.
OSGi.
Eclipse and Oracle | © 2008 Oracle; made available under the EPL v1.0 Building Applications with Eclipse Frameworks and Tools for Oracle Shaun Smith EclipseLink,
MAVEN-BLUEMARTINI Yannick Robin. What is maven-bluemartini?  maven-bluemartini is Maven archetypes for Blue Martini projects  Open source project on.
© 2005 by IBM; made available under the EPL v1.0 | March 1, 2005 Tim deBoer Gorkem Ercan Extend WTP Server Tools for your.
SelfDiagnose “who is to blame” ernest micklei, April 2007.
TDDD05 EJB Lab (Part of slides reused from Mikhail’s) Lu Li
|Tecnologie Web L-A Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat
JBoss Seam Presented by Andy Nguyen Truc Pham. What is JBoss Seam? Created by Gavin King Created by Gavin King A lightweight framework for Java EE 5.0.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Web Applications - Basics. Introduction to Web Web features Clent/Server HyperText Transfer Protocol HyperText Markup Language URL addresses Web server.
Web applications using JavaServer Faces (JSF) A brief introduction 1JavaServer Faces (JSF)
J2EE Structure & Definitions Catie Welsh CSE 432
Testing Web-based applications Typically, a multi-layered architecture is used. Three primary layers, typically on different hosts: –Client: –often uses.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
JavaEE Seams Easy JBoss Seam: A Web Application Component Framework for EJB3, JPA, JSF and AJAX.
Introduction to Web Dimitar Nenchev Ivan Nakov
1 Apache TomEE // JavaEE Web Profile on Tomcat Jonathan #TomEE.
And the PageObject Design Model.  How Selenium Remote Control works  You launch a server on your test machine.  Your tests connect to that server via.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
Enterprise Java Overview Panos Konstantinidis Java Developer JUG Leader & Java Champion
Jonathan Gallimore | Tomitribe Cluster your application with JCache and CDI.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
JAVA EE 6 Best Practices for Migrating Spring to WTF ?!?
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Google Code Libraries Dima Ionut Daniel. Contents What is Google Code? LDAPBeans Object-ldap-mapping Ldap-ODM Bug4j jOOR Rapa jongo Conclusion Bibliography.
Testing Your Alfresco Add-ons Michael Suzuki Software Engineer.
Testing Your Alfresco Add-ons Michael Suzuki Software Engineer.
Apache TomEE Tomcat with a kick. Apache TomEE: Overview ● Java EE 6 Web Profile certification in progress ● Apache TomEE Includes support for: ● Servlet.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Callbacks and Interceptors. Contents  Session Beans Life Cycle  Interceptors.
Overall Architecture and Component Model
Test Driven Development
Testing Your Alfresco Add-ons
Introduction to J2EE Architecture
Java EE Overview The problem Java Enterprise Edition attempts to solve: maximize the use of Java technologies by providing a suite of Java technology specifications.
ISE 390 Dynamic Web Development
Java EE Overview The problem Java Enterprise Edition attempts to solve: maximize the use of Java technologies by providing a suite of Java technology specifications.
Bring your favorite Kafka to Java EE with CDI
Enterprise Java Beans.
Intro to J2EE Concepts.
Testing your DAO’s with Java
Presentation transcript:

Test Java EE applications with Arquillian Ivan St. Ivanov

@ivan_stefanov About nosoftskills.com

@ivan_stefanov

“ The purpose of automated testing is to enable change. Verifying correctness is just a nice side effect. - Jeremy Norris

@ivan_stefanov Java EE Changed a Lot Standalone technologies ◦ EJB container ◦ JPA ◦ CDI Arquillian testing framework

@ivan_stefanov Techniques to test Java EE apps Testing Persistence Testing Contexts and Dependency Injection (CDI) Testing business logic Testing whole scenarios

@ivan_stefanov The showcase app Collects match predictions from registered users Award points for correct predictions Used technologies ◦ Java 8 ◦ Java EE 8 – JPA, CDI, EJB, JAX-RS, JSF Source code:

@ivan_stefanov

Testing Persistence Use embedded databases (HSQLDB, Derby) Covered by other tests, often not needed Used for quick check of persistence code

@ivan_stefanov 1) Create persistence.xml org.hibernate.jpa.HibernatePersistenceProvider

@ivan_stefanov 2) Initialize EntityManager protected static EntityManager public static void setupTestObjects() { EntityManagerFactory emf = Persistence.createEntityManagerFactory( "predcomposer-test"); entityManager = emf.createEntityManager(); }

@ivan_stefanov 3) Begin public void setUp() throws Exception { entityManager.getTransaction().begin(); insertTestData(); entityManager.flush(); this.competitionsService = new CompetitionsService(); competitionsService.entityManager = entityManager; }

public void shouldStoreCompetition() throws Exception { Competition newCompetition = new Competition( "Premiership 2015/2016", "The English Premier League"); Competition storedCompetition = competitionsService.storeCompetition(newCompetition); assertNotNull(storedCompetition.getId()); assertEquals(newCompetition, entityManager.find(Competition.class, storedCompetition.getId())); } 4) Write your test

@ivan_stefanov 5) public void tearDown() { entityManager.getTransaction().rollback(); public static void closeEntityManager() { entityManager.close(); }

@ivan_stefanov

Testing CDI Use CDI Unit or Deltaspike Launches CDI container Easy injection of dependencies, mocks, alternatives Control of requests and sessions Used for quick tests when dependencies and scopes are involved

@ivan_stefanov 1) Add dependency org.jglue.cdi-unit cdi-unit test

public class ViewGamePredictionsBeanTest private ViewGamePredictionsBean public void shouldLoadGamePredictionsUponRequest() { bean.showGamePredictions(game2); assertEquals(2, bean.getPredictions().size()); } } 2) Write the test

public class PredictionsServiceAlternative extends PredictionsService public Set getPredictionsForGame(Game game) { // return two predictions } 3) Create alternative

@ivan_stefanov PredictionsServiceAlternative.class, }) public class ViewGamePredictionsBeanTest { 4) Add the alternative

@ivan_stefanov

Greeting earthlings

@ivan_stefanov Core principles Tests should be portable to any container Tests should be executable from both IDE and build tool The platform should extend existing test frameworks

@ivan_stefanov Step 1 – pick a container Container extensions ◦ JBoss, Tomcat, Weld, Glassfish, Jetty, WebSphere, WebLogic

@ivan_stefanov 1) Add dependencies and profile org.jboss.arquillian.junit arquillian-junit-container test org.wildfly wildfly-arquillian-container-managed test

@ivan_stefanov Step 2 – connect the container Container types ◦ Embedded ◦ Managed ◦ Remote

@ivan_stefanov 2) Configure container target/wildfly Final

@ivan_stefanov Step 3 – package and deploy ShrinkWrap library ◦ Deployment ◦ Resolve from Maven ◦ Create descriptors

public class CompetitionsServiceIntegrationTest public static WebArchive createDeployment() { return ShrinkWrap.create(WebArchive.class).addClass(CompetitionsService.class).addPackage(Prediction.class.getPackage()).addAsResource( new File("src/main/resources/META-INF/persistence.xml"), "META-INF/persistence.xml"); } } 3) Prepare the test archive

@ivan_stefanov Step 4 – run the test Tests runs in-container ◦ CDI, EJB, JNDI available ◦ No need to mock most of the services Present the result as a normal unit test

private CompetitionsService public void shouldCreateCompetition() throws Exception { testCompetition = new Competition("Premiership 2015/2016", "English Premier League"); testGame = new Game("Manchester City", "Juventus", LocalDateTime.of(2015, 9, 15, 21, 45)); testCompetition.getGames().add(testGame); Competition persistedCompetition = competitionsService.storeCompetition(testCompetition); assertNotNull(persistedCompetition.getId()); assertEquals(testCompetition, persistedCompetition); } 4.1) Write the test

@ivan_stefanov public class CompetitionResourceTest public void URL base) { URL url = new URL(base, "rest/competition"); WebTarget target = ClientBuilder.newClient().target(url.toExternalForm()); Form newCompetitionForm = new Form(); newCompetitionForm.param("name", COMPETITION_NAME); newCompetitionForm.param("description", DESCRIPTION); Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(newCompetitionForm, MediaType.APPLICATION_FORM_URLENCODED_TYPE)); assertEquals(201, response.getStatus()); } } 4.2) Client side tests

@ivan_stefanov Step 5 – undeploy the test Undeploy the test archive Disconnect or stop the container

@ivan_stefanov

That’s not all Persistence extension Warp Drone Graphene AngularJS, Android, OSGi …

@ivan_stefanov Graphene extension Drive the application via page navigation Support for AJAX PageObject pattern

@ivan_stefanov 1) Add dependencies org.jboss.arquillian.extension arquillian-drone-bom pom import org.jboss.arquillian.selenium selenium-bom pom import org.jboss.arquillian.graphene graphene-webdriver pom test

@ivan_stefanov 2) Configure extension phantomjs

@ivan_stefanov 3) Create the page public class LoginPage = "loginForm:userName") private WebElement = "loginForm:password") private WebElement = "loginForm:login") private WebElement loginButton; public void login(String userName, String password) { this.userName.sendKeys(userName); this.password.sendKeys(password); guardHttp(loginButton).click(); } }

@ivan_stefanov 4) Create the public class LoginScenarioTest private WebDriver private HomePage public void LoginPage loginPage) { loginPage.login("ivan", "ivan"); homePage.assertGreetingMessage("Ivan"); homePage.assertGameFormVisible(true); } }

@ivan_stefanov

Resources Showcase app Arquillian mocks/