Mocking in Junits & Springboot

Stuti Jain
3 min readMar 11, 2021

--

Unit tests should be small tests (atomic), lightweight, and fast. However, an object under test might have dependencies on other objects. It might need to interact with a database, talk to a web service or a message queue. All these services might not be available during unit testing. Even if they are available, unit testing the object under test along with its dependencies can take unacceptable amount of time.The solution is to use mocking, a way to provide test doubles for your classes being tested.

What is Mockito?

Mockito is a mocking framework for Java. Mockito allows convenient creation of substitutes of real objects for testing purposes.

Step 1: Mocking Objects

What makes a mock object different from the others is that it uses behavior verification. It means that the mock object verifies that the mock object is being used correctly by the object under test.

  • The @Mock annotation creates a mock implementation for the class it is annotated with.
  • @InjectMocks also creates the mock implementation, additionally injects the dependent mocks that are marked with the annotations @Mock into it.

Step 2: @RunWith MockitoJUnitRunner

@RunWith, JUnit framework invokes the specified class as a test runner instead of running the default runner.A MockitoJUnitRunner class automatically initialize all the objects annotated with @Mock and @InjectMocks annotations.

If we are not using the MockitoJUnitRunner class approach, then we can use the static method MockitoAnnotations.initMocks()

Step 3: Stubbing

Stubbing means simulating the behavior of a mock object’s method.

Examples :-

configure simple return behavior for mock

when(roamingPartnerProfileManager.findBySeppFqdn(Mockito.any())).thenReturn(Optional.of(roamingPartnerProfileEntity));

configure return behavior for mock in an alternative way

doReturn(Optional.of(roamingPartnerProfileEntity)).when(roamingPartnerProfileManager).findBySeppFqdn(Mockito.any());

configure mock to throw an exception on a method call

when(methodX()).thenThrow(IllegalStateException.class);

configure the behavior of a method with void return type — to throw an exception

doThrow(NullPointerException.class).when(roamingPartnerProfileManager).getId();

Step 4: Invocation & Assertion

assertEquals(test , controller.onN32fForwardReqRcv(req));

Step 5: Verification

Next we might want to validate that the mocked objects were invoked with expected parameters , number of times it’s invoked & other sequences.

verify(logger,times(1)).warn(Pn32cSeppWarnCodes.RACE_SCENARIO);

Test Case Flow :-

1. From the security negotiate request data get the Sepp FQDN
2. From the retrieved SEPP FQDN validate if context is present
3. If context is present , log a warning that this is a case of race condition.
4. Throw that an Internal Server Exception has occurred & exit gracefully.

To Test Data Layer

Step 1: @RunWith (SpringRunner.class)

It provides a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in our JUnit tests, this annotation will be required.

Step 2: @DataJpaTest

It provides some standard setup needed for testing the persistence layer.

Step 3: @AutoWire TestEntityManager

It provides a subset of database entity manager methods that are useful for tests as well as helper methods for common testing tasks such as persist/flush/find.

Step 4: Validate by retrieving entities

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (1)

Write a response