Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

31
Test it! Unit, mocking and in- container Meet Arquillian! Ivan St. Ivanov

Transcript of Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Page 1: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Test it! Unit, mocking and in-

container Meet Arquillian!

Ivan St. Ivanov

Page 2: 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

Page 3: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 4: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 5: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 6: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 7: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 8: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

CharacteristicsFastRun anywhere

◦IDE◦Build tool◦CI server

RepeatableLeverage known tooling

Page 9: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

LevelsUnitFunctionalIntegrationAcceptance

Page 10: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Unit testingSingle unit of codeIn isolationBenefits:

◦Quick feedback◦Facilitate change◦Document features

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

Page 11: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

“ The purpose of automated

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

- Jeremy Norris

Page 12: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Test frameworks

Page 13: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

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

Page 14: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

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(); }}

Page 15: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 16: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Stubs and mocksUnits are not isolatedStubs

◦Test doubles◦Predetermined behavior

Mocks◦Validate behavior as well

Unit under test implies dependency injection

Page 17: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Dependency injectabilitySample usecase

◦Conference manager◦Cache

Wrong:◦Create Cache inside Conference

manager◦Lookup Cache

Correct◦Constructor or setter injection

Page 18: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Mocking libraries

Page 19: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 20: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Greeting earthlings

Page 21: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Core principlesTests should be portable to any

containerTests should be executable from

both IDE and build toolThe platform should extend

existing test frameworks

Page 22: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Step 1 – pick a containerContainer extensions

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

Page 23: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Step 2 – connect the container

Container types◦Embedded ◦Remote◦Managed

Page 24: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

Step 3 – package and deployShrinkWrap library

◦Deployment◦Resolve from Maven◦Create descriptors

Page 25: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

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

Page 26: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

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

Page 27: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 28: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

That’s not allPersistence extensionWarpDroneGrapheneAngularJS, Android, OSGi…

Page 29: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.
Page 31: Test it! Unit, mocking and in-container Meet Arquillian! Ivan St. Ivanov.

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