Introduction
This example will show you how to mock ObjectMapper.readValue()
using Junit Mockito. When we write Junit test cases or classes, we generally do not test on real object that are injected into the class that has to be tested using Junit to avoid any test case failure at some point, instead we want to test the business logic out of the written code.
We will mock the ObjectMapper
class in the same way we mock our external classes, such as, DAO, Repository or any other classes. Here we will create a simple example to show you how to mock ObjectMapper.readValue()
using Junit Mockito.
Prerequisites
Knowledge of Java, Junit
JDK 1.8, Spring API
Junit API, Jackson API
Example Implementation
Let’s move on to the example implementation…
DTO Class
The below class just holds some information about a user. The getters and setters have been removed from this example but actually these are required to run the example.
public class UserDto {
private String id;
private String name;
private String email;
//getters and setters
@Override
public String toString() {
return "UserDto [id=" + id + ", name=" + name + ", email=" + email + "]";
}
}
Spring Service Class
We create the Spring service class that has only one method and performs the update of user information. We autowired the Jackson’s ObjectMapper
class.
Assuming, the method update()
gets some information from the external source and convert the JSON string into UserDto
and finally does some processing and saves to the permanent storage.
@Service
public class ObjectMapperServiceImpl {
@Autowired
private ObjectMapper objectMapper;
public void updateUser() throws JsonParseException, JsonMappingException, IOException {
// assume below json string value comes from source
final String json = "{\"id\":\"5000605\",\"name\":\"John Miller\",\"email\":\"test@email.com\"}";
UserDto userDto = objectMapper.readValue(json, UserDto.class);
System.out.println(userDto);
// do some processing with userDto and persist to permanent storage
}
}
Junit Test Class
Here is the below Junit class to show how to mock ObjectMapper.readValue()
using Junit Mockito.
Here we mock ObjectMapper
class and notice how we mocked the readValue()
method to return UserDto
instance.
@RunWith(MockitoJUnitRunner.class)
public class ObjectMapperServiceImplTest {
@Mock
private ObjectMapper objectMapper;
@Spy
@InjectMocks
private final ObjectMapperServiceImpl objectMapperServiceImpl = new ObjectMapperServiceImpl();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
@SuppressWarnings("unchecked")
public void testUpdateUser() throws Exception {
UserDto userDto = new UserDto();
userDto.setId("1");
userDto.setName("Soumitra Roy");
userDto.setEmail("soumitra@email.com");
Mockito.when(objectMapper.readValue(Mockito.anyString(), Mockito.any(Class.class))).thenReturn(userDto);
objectMapperServiceImpl.updateUser();
Mockito.verify(objectMapperServiceImpl, Mockito.times(1)).updateUser();
}
}
You may read why you see Mockito.verify()
in the above Junit class in this example.
Thanks for reading.