Published on June 14, 2019
1. Next Level Unit Testing Dominik Poljak, JavaCro, Umag, 14.5.2019. firstname.lastname@example.org
2. Who frequently writes JUnit tests?
3. The ratio of time spent reading (code) versus writing is well over 10 to 1… – Robert C. Martin, Clean Code
4. © ecx.io – an IBM Company Benefits of Unit Testing Code Quality • Exposing edge cases Finding Bugs • bugs are found at early stage Simplified Integration • Better interfaces Improves Debugging • Easier to find issues Provides Documentation • Documentation can be generated
5. How to get the most Value with Minimal Effort?
6. © ecx.io – an IBM Company How to increase value and reduce effort while writing JUnit tests? Increase READABILITY • Introduce common structure Make tests EASY to write • Use powerful and simple libraries Make logic SIMPLE • Make tests independent (use mocking) REUSE tests as documentation • Automate everything you can
7. © ecx.io – an IBM Company Agenda JUnit 4 Test Structure Test Simplification via Mocking Documentation Generation 8
8. JUnit4 Test Structure
9. © ecx.io – an IBM Company JUnit Test Setup Goal: Reusable Configurable Fast How: Common Data Common Objects (Mocks e.g. AemMocks) Utilities
10. What is BDD?
11. © ecx.io – an IBM Company • evolved from TDD • behavior focused • shared language between tech and non-tech teams Behavioral Driven Development (BDD)
12. © ecx.io – an IBM Company Simple BDD Test Structure
13. © ecx.io – an IBM Company 14 AssertJ Fluent assertions for java
14. © ecx.io – an IBM Company 15 AssertJ Fluent assertions for java
15. Unit Test Simplification
16. Mockito: mocking method call
17. Mockito: mocking method with different return values on consecutive calls
18. Mockito: using SPY
19. How close to 100% code coverage? Successfully mocked objects!
20. Powermock: Mock System Classes
21. Powermock: Mock Static Methods
22. Powermock: Mock Constructor Call
23. © ecx.io – an IBM Company Motivation: Constructor/method loads dll file or accesses network which makes code "untestable". Supressing: • constructor of a superclass / own constructor and instantiating a class • Removing static initializer for the class • specific method / field initialization
24. Powermock: Bypass Encapsulation
25. © ecx.io – an IBM Company Motivation: automate common mocking tasks – Mock Policy OOTB Mock Policy: • @MockPolicy(Slf4jMockPolicy.class) – removes unnecessary logging, improves test/build performance and readability
26. Documentation Generation
27. Documentation created from JUnit?
28. © ecx.io – an IBM Company Why JGiven? Java code using a fluent API JUnit or TestNG support Modular way of writing Scenarios Reports are generated for domain experts
29. © ecx.io – an IBM Company JGiven 34
30. © ecx.io – an IBM Company Stages Example 35
31. © ecx.io – an IBM Company Scenario Example 36
32. © ecx.io – an IBM Company JGiven Annotations Custom Text @As @ExtendedDescription @Format Multiple objects in table format State Sharing between Stages @ScenarioState(resolution = Resolution.NAME) Test setup (injection) @BeforeScenario, @BeforeStage Attachments (screenshot from Selenium) 37
34. Questions? That’s it.