Testing
OUTLINE
- Unit Testing
- JUnit 5
- Mocking
- Mockito
- Code Coverage
- Jacoco
- Test Driven Development
1 Testing
1.1 What is testing?
- An important later stage in SDLC
- Helps us to check if code logic perform as expected
Two Main Types
- Unit Testing: testing of individual behaviours on different functional groups within an application
- Integration Testing: testing correct overall functionality of multiple functional groups interacting within an application
1.2 Why Testing?
Unit Test serves multiple purposes:
- Quality of Code: ensures existing logic not broken by additional features as application grows
- Documentation: test cases are another place for system behaviour to be recorded
- TDD: when writing test cases before actual code, ensures developer thinks thoroughly about business requirements and logics.
1.3 How to testing?
Come up with test cases that try to cover enough scenarios:
- Happy Path Test Case
- Exception Path Test Case
- Edge Cases
Write deterministic test cases: the test result should not change without change in the code or test.
In another words, hard code your test cases and avoid using random values in your test cases for now
2 JUnit 5
JUnit testing framework is widely used to perform unit testing on Java projects
JUnit 5 provides support for Java 8 features, introduced the new extension model, as while as other features comparing to JUnit 4.
Annotations are different between the two:
- @Before annotation is renamed to @BeforeEach
- @After annotation is renamed to @AfterEach
- @BeforeClass annotation is renamed to @BeforeAll
- @AfterClass annotation is renamed to @AfterAll
- @Ignore annotation is renamed to @Disabled