test it! unit, mocking and in-container meet arquillian!

Post on 29-Jun-2015

542 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

A gentle introduction into testing in Java. Begin with testing a single unit, continue with mocking dependency and end up with a full incontainer test capabilities.

TRANSCRIPT

Test it! Unit, mocking and in-

container Meet Arquillian!

Ivan St. Ivanov

AgendaIntroduction to testingPure unit testing with JUnitStubs and mocks. Mockito libraryIn-container testing with

Arquillian

CharacteristicsFastRun anywhere

◦IDE◦Build tool◦CI server

RepeatableLeverage known tooling

LevelsUnitFunctionalIntegrationAcceptance

Unit testingSingle unit of codeIn isolationBenefits:

◦Quick feedback◦Facilitate change◦Document features

Limitations◦Won’t catch integration errors◦Too much mocking

“ The purpose of automated

testing is to enable change. Verifying correctness is just a nice side effect.

- Jeremy Norris

Test frameworks

Introduction to JUnitNo need to extend framework

classesAnnotate test methods with

@TestPreparation methods with

@Before and @BeforeClassCleanup activities with @After

and @AfterClassCheck with Assert methodsExtensible with test runners

Example JUnit test casepublic class FooBarTest {

private Foo foo;

@Before public void setup() { foo = new Foo(); foo.init(); }

@Test public void testBar() throws Exception { Assert.assertEquals("bar", foo.getBar()); }

@After public void tearDown() { foo.cleanup(); }}

Stubs and mocksUnits are not isolatedStubs

◦Test doubles◦Predetermined behavior

Mocks◦Validate behavior as well

Unit under test implies dependency injection

Dependency injectabilitySample usecase

◦Conference manager◦Cache

Wrong:◦Create Cache inside Conference

manager◦Lookup Cache

Correct◦Constructor or setter injection

Mocking libraries

Greeting earthlings

Core principlesTests should be portable to any

containerTests should be executable from

both IDE and build toolThe platform should extend

existing test frameworks

Step 1 – pick a containerContainer extensions

◦JBoss, Tomcat, Weld, Glassfish, Jetty, WebSphere, WebLogic

Step 2 – connect the container

Container types◦Embedded ◦Remote◦Managed

Step 3 – package and deployShrinkWrap library

◦Deployment◦Resolve from Maven◦Create descriptors

Step 4 – run the testTests runs in-container

◦CDI, EJB, JNDI available◦No need to mock most of the

servicesPresent the result as a normal

unit test

Step 5 – undeploy the testUndeploy the test archiveDisconnect or stop the container

That’s not allPersistence extensionWarpDroneGrapheneAngularJS, Android, OSGi…

About this demoSlideshttp://bit.ly/bgjug-testing-slides Showcase – initialhttp://bit.ly/bgjug-testing Showcase – finalhttp://bit.ly/bgjug-testing-final

top related