• Like
Pragmatic Java Test Automation
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Pragmatic Java Test Automation



  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Java Test Automation Real-world patterns and practices Dmitry Buzdin
  • 3. 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
  • 4. 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
  • 5. Testing patterns in thispresentation are applicable to any framework/system
  • 7. Typical Layers User Interface Application Services Domain ModelIntegration ComponentsExternal Services & Storage
  • 8. Our RequirementsDisplay weather forecastsStore them in databaseLots of other usual requirements Cloud, HTML5, BigData...
  • 9. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
  • 10. Technology StackFront-end - Twitter Bootstrap + jQueryREST - JAX-RS 1.1 / JerseyDependency Injection - CDI / WeldORM - JPA 2.0 / HibernateSQL DB - PostgreSQL
  • 11. Source of weather data - REST APIhttp://api.openweathermap.org/data/2.1/find/name? q=riga
  • 12. DEMO
  • 14. Unit TestsIntegration TestsFunctional Tests
  • 16. Unit TestsClasses are tested in isolation(almost)One test method checks oneuse-caseOne class <-> one test class
  • 17. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
  • 18. 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/
  • 19. Use Dependency Injection Separation of classes Mostly about testing Popular DI frameworks Spring CDI Guice
  • 20. Injection Typesby constructorby setterby field
  • 21. 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/
  • 22. 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/
  • 23. BenefitsForget assertEquals() !Hamcrest matchers are expressive flexible extendable
  • 24. DEMO
  • 26. Integration TestsShould not start the whole applicationTesting integration components Remote API calls Data conversion Fault-scenarios
  • 27. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
  • 28. Fake DependenciesWrite code for emulating dependenciesFake Web ServiceFake FTP serverFake InputStreamFake XML response
  • 29. DEMO
  • 30. Persistence TestsIsolate and test all persistenceoperationsIdeally all CRUD operationsCould be done in Generic way
  • 31. In-memory StorageTransient database for test executionSome DBs have it built-inFor SQL DB mocking pick H2 Fast Emulation modes http://www.h2database.com/
  • 32. DEMO
  • 34. Functional TestsNot testing UIStarting application contextEmulating multiple user requestsPersisting intermediate results
  • 35. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
  • 36. Embedded ContainerTest should start applicationIt is possible to start embedded CDI Container EJB Container Spring Container
  • 37. Functional Testing using Service Layer Ignoring GUI data conversion Easier to test Faster test execution
  • 38. Mock IntegrationsCould be replaced using Properties and factory beans Spring @Profile CDI @Alternative Guice Modules
  • 39. Environment SwitchApplication should run in several modesDone with System environment variableTypical modes: production local deployment embedded testing
  • 40. Configuration OverrideEnvironments overrides settingsHierarchical configurationsProperties, YAML or other
  • 41. Initial DataReset database after every test andinsert initial dataReuse ORM mapping or other persistencelayerOr use specialized tools like DbUnit
  • 42. API-Level TestingWrite test for your APISome examples: SOAP REST EJB
  • 43. DEMO
  • 44. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather OtherSQL DB REST API APIs
  • 45. Embedded Web ServerIt is possible to run embedded Jetty Glassfish/Grizzly
  • 46. JUnit Rules@Rulepublic EmbeddedJetty jetty = newEmbeddedJetty();@Testpublic void shouldTestEmbeddedJetty() { ...}
  • 47. DEMO
  • 48. SUMMARY
  • 49. Why is it all needed?Decrease cost of changeIncrease software qualityRemove fear of making changes
  • 50. When Testing is Enough? If CI build passes it is safe to deploy to production
  • 51. What was not covered? Automated Acceptance testing GUI-level testing Performance testing
  • 52. Dmitry BuzdinFreelance Software Architect http://buzdin.lv buzdin@gmail.com @buzdin