With unit testing, as opposed to integration testing, we aim to test each class independently. However, many classes have dependencies that we don't want to rely on. So we use mocks.
Mocks are smart objects whose output can vary depending on the input. Mockito is the most popular mocking framework with a concise, yet easy to grasp, syntax.
We'll mock the StringUtil
class with its concat()
method concatenating two String
objects:
public class StringUtil { public String concat(String a, String b) { return a + b; } }