Pragmatic Java Test Automation
Upcoming SlideShare
Loading in...5
×
 

Pragmatic Java Test Automation

on

  • 3,476 views

 

Statistics

Views

Total Views
3,476
Slideshare-icon Views on SlideShare
3,229
Embed Views
247

Actions

Likes
8
Downloads
41
Comments
0

10 Embeds 247

http://413x4nd3r.blogspot.com 125
http://jug.lv 88
http://413x4nd3r.blogspot.ru 25
http://feeds.feedburner.com 2
https://www.linkedin.com 2
http://413x4nd3r.blogspot.sk 1
https://twitter.com 1
http://413x4nd3r.blogspot.de 1
http://www.413x4nd3r.blogspot.ru 1
http://413x4nd3r.blogspot.fi 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Pragmatic Java Test Automation Pragmatic Java Test Automation Presentation Transcript

    • Java Test Automation Real-world patterns and practices Dmitry Buzdin
    • TESTINGAUTOMATION
    • Wat iz test automation? Tests are automated! Tests could be run on developer laptop Tests are running in CI on regular basis Tests do not require network access
    • Signs that there is not enough automation Starting GUI to check backend changes Deploying to server to check functionality Relying on debug logging Testing is for testers You do not sleep well
    • Testing patterns in thispresentation are applicable to any framework/system
    • SAMPLE APPLICATION
    • Typical Layers User Interface Application Services Domain ModelIntegration ComponentsExternal Services & Storage
    • Our RequirementsDisplay weather forecastsStore them in databaseLots of other usual requirements Cloud, HTML5, BigData...
    • Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
    • Technology StackFront-end - Twitter Bootstrap + jQueryREST - JAX-RS 1.1 / JerseyDependency Injection - CDI / WeldORM - JPA 2.0 / HibernateSQL DB - PostgreSQL
    • Source of weather data - REST APIhttp://api.openweathermap.org/data/2.1/find/name? q=riga
    • DEMO
    • TESTING STRATEGY
    • Unit TestsIntegration TestsFunctional Tests
    • UNIT TESTING
    • Unit TestsClasses are tested in isolation(almost)One test method checks oneuse-caseOne class <-> one test class
    • Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
    • JUnit Tests@Beforepublic void setUp() { // Preparing object for test}@Testpublic void shouldFindWeatherByCity() throws Exception { // Conditions set-up // Method under test invocation // Assertions} http://junit.org/
    • Use Dependency Injection Separation of classes Mostly about testing Popular DI frameworks Spring CDI Guice
    • Injection Typesby constructorby setterby field
    • Mockito Mocks// Mocking all class dependenciesservice = new WeatherServiceImpl();service.weatherSource = Mockito.mock(WeatherSource.class);// Sets-up mock reactionwhen(service.weatherSource.findByCityName(eq("Kolka"))) .thenReturn(expectedResult);// Your method under test here// Verifies interactions with mockverify(service.entityManager) .persist(any(Temperature.class)); http://code.google.com/p/mockito/
    • Hamcrest MatchersassertThat(ages, everyItem(greaterThan(18)));assertThat(param, equalTo(42));assertThat(param, notNullValue());assertThat(object, is(String.class))assertThat(object, anyOf(is(String.class), is(Integer.class)) http://code.google.com/p/hamcrest/
    • BenefitsForget assertEquals() !Hamcrest matchers are expressive flexible extendable
    • DEMO
    • INTEGRATION TESTING
    • Integration TestsShould not start the whole applicationTesting integration components Remote API calls Data conversion Fault-scenarios
    • Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
    • Fake DependenciesWrite code for emulating dependenciesFake Web ServiceFake FTP serverFake InputStreamFake XML response
    • DEMO
    • Persistence TestsIsolate and test all persistenceoperationsIdeally all CRUD operationsCould be done in Generic way
    • In-memory StorageTransient database for test executionSome DBs have it built-inFor SQL DB mocking pick H2 Fast Emulation modes http://www.h2database.com/
    • DEMO
    • FUNCTIONAL TESTING
    • Functional TestsNot testing UIStarting application contextEmulating multiple user requestsPersisting intermediate results
    • Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
    • Embedded ContainerTest should start applicationIt is possible to start embedded CDI Container EJB Container Spring Container
    • Functional Testing using Service Layer Ignoring GUI data conversion Easier to test Faster test execution
    • Mock IntegrationsCould be replaced using Properties and factory beans Spring @Profile CDI @Alternative Guice Modules
    • Environment SwitchApplication should run in several modesDone with System environment variableTypical modes: production local deployment embedded testing
    • Configuration OverrideEnvironments overrides settingsHierarchical configurationsProperties, YAML or other
    • Initial DataReset database after every test andinsert initial dataReuse ORM mapping or other persistencelayerOr use specialized tools like DbUnit
    • API-Level TestingWrite test for your APISome examples: SOAP REST EJB
    • DEMO
    • Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
    • Embedded Web ServerIt is possible to run embedded Jetty Glassfish/Grizzly
    • JUnit Rules@Rulepublic EmbeddedJetty jetty = newEmbeddedJetty();@Testpublic void shouldTestEmbeddedJetty() { ...}
    • DEMO
    • SUMMARY
    • Why is it all needed?Decrease cost of changeIncrease software qualityRemove fear of making changes
    • When Testing is Enough? If CI build passes it is safe to deploy to production
    • What was not covered? Automated Acceptance testing GUI-level testing Performance testing
    • Dmitry BuzdinFreelance Software Architect http://buzdin.lv buzdin@gmail.com @buzdin