Date post: | 19-Jan-2016 |
Category: |
Documents |
Upload: | camilla-jennings |
View: | 217 times |
Download: | 0 times |
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…
ResourcesTestinghttp://en.wikipedia.org/wiki/Software_testingUnit testinghttp://en.wikipedia.org/wiki/Unit_testing Mocks and stubshttp://
martinfowler.com/articles/mocksArentStubs.html Arquillianhttp://aslakknutsen.github.io/presentations/https://rpestano.wordpress.com/2014/06/08/arquillian/https://rpestano.wordpress.com/2014/10/25/arquillian-and-mocks
/
About this demoSlideshttp://bit.ly/bgjug-testing-slides Showcase – initialhttp://bit.ly/bgjug-testing Showcase – finalhttp://bit.ly/bgjug-testing-final