Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Pragmatic Java Test Automation

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Nächste SlideShare
Testing In Java
Testing In Java
Wird geladen in …3
×

Hier ansehen

1 von 52 Anzeige
Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

Andere mochten auch (20)

Anzeige

Ähnlich wie Pragmatic Java Test Automation (20)

Weitere von Dmitry Buzdin (20)

Anzeige

Pragmatic Java Test Automation

  1. 1. Java Test Automation Real-world patterns and practices Dmitry Buzdin
  2. 2. TESTING AUTOMATION
  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 this presentation are applicable to any framework/system
  6. 6. SAMPLE APPLICATION
  7. 7. Typical Layers User Interface Application Services Domain Model Integration Components External Services & Storage
  8. 8. Our Requirements Display weather forecasts Store them in database Lots of other usual requirements Cloud, HTML5, BigData...
  9. 9. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather Other SQL DB REST API APIs
  10. 10. Technology Stack Front-end - Twitter Bootstrap + jQuery REST - JAX-RS 1.1 / Jersey Dependency Injection - CDI / Weld ORM - JPA 2.0 / Hibernate SQL DB - PostgreSQL
  11. 11. Source of weather data - REST API http://api.openweathermap.org/data/2.1/find/name? q=riga
  12. 12. DEMO
  13. 13. TESTING STRATEGY
  14. 14. Unit Tests Integration Tests Functional Tests
  15. 15. UNIT TESTING
  16. 16. Unit Tests Classes are tested in isolation (almost) One test method checks one use-case One class <-> one test class
  17. 17. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather Other SQL DB REST API APIs
  18. 18. JUnit Tests @Before public void setUp() { // Preparing object for test } @Test public 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 Types by constructor by setter by field
  21. 21. Mockito Mocks // Mocking all class dependencies service = new WeatherServiceImpl(); service.weatherSource = Mockito.mock(WeatherSource.class); // Sets-up mock reaction when(service.weatherSource.findByCityName(eq("Kolka"))) .thenReturn(expectedResult); // Your method under test here // Verifies interactions with mock verify(service.entityManager) .persist(any(Temperature.class)); http://code.google.com/p/mockito/
  22. 22. Hamcrest Matchers assertThat(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. Benefits Forget assertEquals() ! Hamcrest matchers are expressive flexible extendable
  24. 24. DEMO
  25. 25. INTEGRATION TESTING
  26. 26. Integration Tests Should not start the whole application Testing integration components Remote API calls Data conversion Fault-scenarios
  27. 27. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather Other SQL DB REST API APIs
  28. 28. Fake Dependencies Write code for emulating dependencies Fake Web Service Fake FTP server Fake InputStream Fake XML response
  29. 29. DEMO
  30. 30. Persistence Tests Isolate and test all persistence operations Ideally all CRUD operations Could be done in Generic way
  31. 31. In-memory Storage Transient database for test execution Some DBs have it built-in For SQL DB mocking pick H2 Fast Emulation modes http://www.h2database.com/
  32. 32. DEMO
  33. 33. FUNCTIONAL TESTING
  34. 34. Functional Tests Not testing UI Starting application context Emulating multiple user requests Persisting intermediate results
  35. 35. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather Other SQL DB REST API APIs
  36. 36. Embedded Container Test should start application It 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 Integrations Could be replaced using Properties and factory beans Spring @Profile CDI @Alternative Guice Modules
  39. 39. Environment Switch Application should run in several modes Done with System environment variable Typical modes: production local deployment embedded testing
  40. 40. Configuration Override Environments overrides settings Hierarchical configurations Properties, YAML or other
  41. 41. Initial Data Reset database after every test and insert initial data Reuse ORM mapping or other persistence layer Or use specialized tools like DbUnit
  42. 42. API-Level Testing Write test for your API Some examples: SOAP REST EJB
  43. 43. DEMO
  44. 44. Web Client REST API Service Layer Domain Model ORM Integration Layer Weather Other SQL DB REST API APIs
  45. 45. Embedded Web Server It is possible to run embedded Jetty Glassfish/Grizzly
  46. 46. JUnit Rules @Rule public EmbeddedJetty jetty = new EmbeddedJetty(); @Test public void shouldTestEmbeddedJetty() { ... }
  47. 47. DEMO
  48. 48. SUMMARY
  49. 49. Why is it all needed? Decrease cost of change Increase software quality Remove 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 Buzdin Freelance Software Architect http://buzdin.lv buzdin@gmail.com @buzdin

×