How to cheat on unit testing to achieve coverage and why you should not
Test coverage is a useful tool for making sure the unit tests are good enough. However, if they are used without being conscious of this fact, without good test knowledge, or without fully understanding why tests are needed in the first place, they will not be as useful as intended by their creators.
For example, imagine code for testing a calculator, as follows:
Calculator.java
package chapter2; public class Calculator { public int add(int number1, int number2) { return number1 + number2; } }
To make the coverage pass with 100% accuracy, we simply need to make sure we call the add
method with any two numbers.
The following is an example of such code:
CalculatorTest.java
package chapter2; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*;class CalculatorTest { &...