Download presentation
Presentation is loading. Please wait.
Published byAdrian Terry Modified over 8 years ago
1
Test Java EE applications with Arquillian Ivan St. Ivanov
2
@ivan_stefanov About me @ivan_stefanov nosoftskills.com
3
@ivan_stefanov
4
“ The purpose of automated testing is to enable change. Verifying correctness is just a nice side effect. - Jeremy Norris
5
@ivan_stefanov Java EE Changed a Lot Standalone technologies ◦ EJB container ◦ JPA ◦ CDI Arquillian testing framework
6
@ivan_stefanov Techniques to test Java EE apps Testing Persistence Testing Contexts and Dependency Injection (CDI) Testing business logic Testing whole scenarios
7
@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: https://github.com/ivannov/predcomposer
8
@ivan_stefanov
9
Testing Persistence Use embedded databases (HSQLDB, Derby) Covered by other tests, often not needed Used for quick check of persistence code
10
@ivan_stefanov 1) Create persistence.xml org.hibernate.jpa.HibernatePersistenceProvider
11
@ivan_stefanov 2) Initialize EntityManager protected static EntityManager entityManager; @BeforeClass public static void setupTestObjects() { EntityManagerFactory emf = Persistence.createEntityManagerFactory( "predcomposer-test"); entityManager = emf.createEntityManager(); }
12
@ivan_stefanov 3) Begin transaction @Before public void setUp() throws Exception { entityManager.getTransaction().begin(); insertTestData(); entityManager.flush(); this.competitionsService = new CompetitionsService(); competitionsService.entityManager = entityManager; }
13
@ivan_stefanov @Test 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
14
@ivan_stefanov 5) Cleanup @After public void tearDown() { entityManager.getTransaction().rollback(); } @AfterClass public static void closeEntityManager() { entityManager.close(); }
15
@ivan_stefanov
16
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
17
@ivan_stefanov 1) Add dependency org.jglue.cdi-unit cdi-unit 3.1.2 test
18
@ivan_stefanov @RunWith(CdiRunner.class) public class ViewGamePredictionsBeanTest { @Inject private ViewGamePredictionsBean bean; @Test public void shouldLoadGamePredictionsUponRequest() { bean.showGamePredictions(game2); assertEquals(2, bean.getPredictions().size()); } } 2) Write the test
19
@ivan_stefanov @Alternative public class PredictionsServiceAlternative extends PredictionsService { @Override public Set getPredictionsForGame(Game game) { // return two predictions } 3) Create alternative
20
@ivan_stefanov @RunWith(CdiRunner.class) @ActivatedAlternatives({ PredictionsServiceAlternative.class, }) public class ViewGamePredictionsBeanTest { 4) Add the alternative
21
@ivan_stefanov
22
Greeting earthlings
23
@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
24
@ivan_stefanov Step 1 – pick a container Container extensions ◦ JBoss, Tomcat, Weld, Glassfish, Jetty, WebSphere, WebLogic
25
@ivan_stefanov 1) Add dependencies and profile org.jboss.arquillian.junit arquillian-junit-container test org.wildfly wildfly-arquillian-container-managed test
26
@ivan_stefanov Step 2 – connect the container Container types ◦ Embedded ◦ Managed ◦ Remote
27
@ivan_stefanov 2) Configure container target/wildfly-9.0.1.Final
28
@ivan_stefanov Step 3 – package and deploy ShrinkWrap library ◦ Deployment ◦ Resolve from Maven ◦ Create descriptors
29
@ivan_stefanov @RunWith(Arquillian.class) public class CompetitionsServiceIntegrationTest { @Deployment 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
30
@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
31
@ivan_stefanov @Inject private CompetitionsService competitionsService; @Test 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
32
@ivan_stefanov @RunWith(Arquillian.class) @RunAsClient public class CompetitionResourceTest { @Test public void shouldCreateCompetition(@ArquillianResource 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
33
@ivan_stefanov Step 5 – undeploy the test Undeploy the test archive Disconnect or stop the container
34
@ivan_stefanov
35
That’s not all Persistence extension Warp Drone Graphene AngularJS, Android, OSGi …
36
@ivan_stefanov Graphene extension Drive the application via page navigation Support for AJAX PageObject pattern
37
@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
38
@ivan_stefanov 2) Configure extension phantomjs
39
@ivan_stefanov 3) Create the page object @Location("login.jsf") public class LoginPage { @FindBy(id = "loginForm:userName") private WebElement userName; @FindBy(id = "loginForm:password") private WebElement password; @FindBy(id = "loginForm:login") private WebElement loginButton; public void login(String userName, String password) { this.userName.sendKeys(userName); this.password.sendKeys(password); guardHttp(loginButton).click(); } }
40
@ivan_stefanov 4) Create the scenario test @RunWith(Arquillian.class) @RunAsClient public class LoginScenarioTest { @Drone private WebDriver browser; @Page private HomePage homePage; @Test public void shouldSayHelloUponSuccessfulLogin( @InitialPage LoginPage loginPage) { loginPage.login("ivan", "ivan"); homePage.assertGreetingMessage("Ivan"); homePage.assertGameFormVisible(true); } }
41
@ivan_stefanov
43
Resources Showcase app https://github.com/ivannov/predcomposer Arquillian http://aslakknutsen.github.io/presentations/ https://rpestano.wordpress.com/2014/06/08/arquillian/ https://rpestano.wordpress.com/2014/10/25/arquillian-and- mocks/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.