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 bas...
Signs that there is not  enough automation Starting GUI to check backend changes Deploying to server to check functionalit...
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    ...
Technology StackFront-end - Twitter Bootstrap + jQueryREST - JAX-RS 1.1 / JerseyDependency Injection - CDI / WeldORM - JPA...
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    ...
JUnit Tests@Beforepublic void setUp() {   // Preparing object for test}@Testpublic   void shouldFindWeatherByCity() throws...
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(Weat...
Hamcrest MatchersassertThat(ages, everyItem(greaterThan(18)));assertThat(param, equalTo(42));assertThat(param, notNullValu...
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  ...
Web Client          REST API         Service Layer         Domain Model ORM         Integration Layer          Weather    ...
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...
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    ...
Embedded ContainerTest should start applicationIt is possible to start embedded  CDI Container  EJB Container  Spring Cont...
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  ...
Configuration OverrideEnvironments overrides settingsHierarchical configurationsProperties, YAML or other
Initial DataReset database after every test andinsert initial dataReuse ORM mapping or other persistencelayerOr use specia...
API-Level TestingWrite test for your APISome examples: SOAP REST EJB
DEMO
Web Client          REST API         Service Layer         Domain Model ORM         Integration Layer          Weather    ...
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
Upcoming SlideShare
Loading in...5
×

Pragmatic Java Test Automation

5,310

Published on

0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,310
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
75
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Pragmatic Java Test Automation

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

    Clipping is a handy way to collect important slides you want to go back to later.

×