Mock everything in Java/Spring with Mockito


Introduction

If you’re talking about the quality of the software, this test is irreplaceable and should not be overlooked, and for developers, the unit test is as important as we need (should) do to test what we develop. It is very important to test the module /method/function, which is indispensable to other related mocks so that we can test only what we are interested in. In the world of Java and Spring, the most popular mock tool is Mockito itself, a framework that mocks everything that is (a).

What is Mockito?

It’s a framework for the Unit Test in Java which allows us to write tests with clean & simple API, Mockito won’t make us puzzled because tests written with Mockito are easy to read and verify.

Mockito’s framework supports dependency management for both Maven and Gradle.

Here, we use Mockito to design a unit test. Following are the steps which will help you do that.

Create a Class for Test

We will test the FooService class with Mockito in the method to call the methods of FooComponent, which is the part we will mock ourselves.processSomething

class FooService
class FooComponent

Use Mockito Test

For the Unit Test of the FooService class, we test the method test.

processSomething

@RunWith(MockitoJUnitRunner.class) is an annotation that says this class will use MockitoJUnitRunner to test.

@Mock is an annotation that indicates that it will mock this field of test classes, which we determine whether to mock the method in the test method.

@InjectMocks is an annotation that identifies as a field for testing. Which actually runs the method to trigger.

@Spy is an annotation that indicates that some mock method is called, but it can still verify it.

@Test we assign the method as a Test method of JUnit.

In the Mock Section,

when Method is a mock, the non-void method of the field that specifies that when this method is called will continue to do something here, which will be used to return the value, or maybe any throw.@MockthenReturnexception

doNothing The method is a mock void method of the field that identifies @Mock that when the void method is rubbed with this argument, you can use the

This argument is used for this mock that matches with the method that is called to use.

  • any() For anything, that’s an argument.
  • anyString() For strings only as argument, it also uses any of the other data type.
  • any(Bar.class) For classes specified in any method only, which is a class of anything (in this example, use the Bar class).

In the Verify section,

We use verify method is to verify that the mock field, with various verification mode (in this example used, and atLeastOnce()times(x))

  • times(x) — X number has been called.
  • never() — It’s not called at all.
  • atLeastOnce() — At least once called
  • atLeast(x) — At least x call
  • atMost(x) — Most triggered x times

What to do for Mockito

  • Do not mock types that do not use ours.
  • Do not mock value objects.
  • Don’t mock everything.
  • Show love with tests

Conclusion

Using the popular Mockito framework for a simple mock service, we can mock everything, including mock exceptions, allowing three-vehicle developers to test every case and scenario efficiently and productively.