Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Globallogic)

18 Aufrufe

Veröffentlicht am

- Why Spring Boot for REST?
- Why bother write tests?
- Spring Test Framework Overview
- Reviewing Test Results

Veröffentlicht in: Ingenieurwesen
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Globallogic)

  1. 1. 1 Confidential 1
  2. 2. Confidential Testing REST with Spring Kostiantyn Baranov Senior Software Engineer 2
  3. 3. 3 Confidential ● 6 years at GL ● Trainer & Mentor at GL Training Center ● Works with Microservice architecture based on Spring and Spring Boot, Gradle & Docker with AWS About myself
  4. 4. 4 Confidential ● Why Spring Boot for REST ● Why bother write tests? ● Spring Test Framework Overview ● Reviewing Test Results Agenda
  5. 5. 5 Confidential ● Spring Test Framework ● jUnit 4 ● Spring RestDocs ● Allure Demo
  6. 6. 6 Confidential REST with Spring Boot
  7. 7. 7 Confidential Spring Ecosystem
  8. 8. 8 Confidential Spring MVC Framework ● Supports RESTful URLs ● Annotation based configuration ● Flexible in supporting different Views
  9. 9. 9 Confidential ● Based on Spring MVC ● JSON/XML/Hypermedia Support ● Annotations like @RestController, @ResponseBody, @RequesBody ● RestTemplate Spring Rest Support
  10. 10. 10 Confidential ● Create stand-alone Spring applications ● Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files) ● Easy Dependency Management & Automatically configure Spring and 3rd party libraries whenever possible ● Provide production-ready features such as metrics, health checks and externalized configuration Spring Boot features
  11. 11. 11 Confidential Auto Configuration
  12. 12. 12 Confidential JetBrains 2019 12
  13. 13. 13 Confidential JetBrains 2019 13
  14. 14. 14 Confidential EJB 3.0 EJB 3.0 & SOAP EJB 2.0 SOAP
  15. 15. 15 Confidential Why bother write tests?
  16. 16. 16 Confidential 1. It costs a lot 2. It’s boring 3. It’s working anyway 4. Don’t you test in production? Why you shouldn’t write tests
  17. 17. 17 Confidential ● Better than frequent fire in prod ● Profit for the team ● It’s YOUR problem Why you !!should!! write tests
  18. 18. 18 Confidential Maintainability
  19. 19. 19 Confidential ● Understanding of requirements ● Refactoring insurance ● Documenting features ● Improving API ● Reducing errors ● Confirming code is working on review Advantages to dev
  20. 20. 20 Confidential ● Branch Pipelines ● Build Pipelines ● Deploy Pipelines Testing Automation DevOps
  21. 21. 21 Confidential ● You are not allowed to write any production code unless it is to make a failing unit test pass. ● You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. ● You are not allowed to write any more production code than is sufficient to pass the one failing unit test. The Three Laws of TDD
  22. 22. 22 Confidential Code first, no time to think
  23. 23. 23 Confidential Test Pyramid
  24. 24. 24 Confidential The goal of unit testing, is to validate that each unit of the software performs as designed. A unit is the smallest testable part of any software. Goal of Unit Testing
  25. 25. 25 Confidential The goal of integration testing, is to test whether many separately developed modules work together as expected Goal of Integration Testing
  26. 26. 26 Confidential ● Give us confidence that the code does what it should. ● Provide feedback that is fast, accurate, reliable and predictable ● Make maintenance easier, something that is commonly overlooked when writing tests. As summary
  27. 27. 27 Confidential Spring Test Framework Overview
  28. 28. 28 Confidential ● SpringRunner for jUnit 4 ● @MockMVC & @WebMvcTest ● @RestClientTest & TestRestTemplate ● @ContextConfiguration & @WebAppConfiguration ● @DataJpaTest & @SpringBootTest ● @TestConfiguration ● @TestPropertySource ● @ActiveProfiles ● @ContextHierarchy ● @DirtiesContext ● @TestExecutionListeners ● JsonPath ● ….. Spring Test
  29. 29. 29 Confidential ● @ContextConfiguration ● @WebAppConfiguration ● @TestConfiguration ● @ActiveProfiles ● @ContextHierarchy ● @TestPropertySource ● @DirtiesContext Spring Test Configuration
  30. 30. 30 Confidential ● @SpringBootTest ● @ActiveProfiles ● @TestPropertySource ● @Import ● @ContextConfiguration Annotations which impact on context
  31. 31. 31 Confidential Once the TestContext framework loads an ApplicationContext (or WebApplicationContext) for a test, that context is cached and reused for all subsequent tests that declare the same unique context configuration within the same test suite. Context Caching
  32. 32. 32 Confidential DEMO
  33. 33. 33 Confidential ● Using profiles wisely ● Reconsidering usage of @MockBean ● Thinking carefully about @DirtiesContext ● Using test slices - @WebMvcTest & @DataJpaTest, etc ● Using class inheritance ● Refactoring into unit tests Spring Test Performance Optimization
  34. 34. 34 Confidential Usual test configuration with Spring Test
  35. 35. 35 Confidential Writing custom Spring test code feels like
  36. 36. 36 Confidential Reviewing Test Results
  37. 37. 37 Confidential ● curl and http request snippets are generated ● easy to package documentation in projects jar file ● easy to add extra information to the snippets ● supports both JSON & XML ● keep up docs with your up-to-date functionality ● encourage to work by TDD Spring Rest Docs
  38. 38. 38 Confidential Spring Rest Docs
  39. 39. 39 Confidential Allure
  40. 40. 40 Confidential DEMO
  41. 41. 41 Confidential One more thing
  42. 42. 42 Confidential ● Spring Boot 1.x EOL Aug 1st 2019 ● Time to upgrade It is time - Goodbye Spring Boot 1.x!
  43. 43. 43 Confidential ● https://mockaroo.com/ ● https://github.com/Codearte/jfairy ● https://github.com/six2six/fixture-factory ● https://github.com/DiUS/java-faker Test Data Fakers
  44. 44. 44 Confidential
  45. 45. 45 Confidential Final thought
  46. 46. 46 Confidential How it feels presenting after 2 Senior Consultants
  47. 47. 47 Confidential Q/A
  48. 48. 48 Confidential Q/A
  49. 49. 49 Confidential Thank you

×