如何使用Mockito模拟ObjectMapper.readValue() [英] How to mock ObjectMapper.readValue() using mockito

查看:491
本文介绍了如何使用Mockito模拟ObjectMapper.readValue()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试服务层,但不确定如何在该类中模拟ObjectMapper().readValue.我对mockito相当陌生,可以弄清楚该怎么做.

I'm testing a service layer and not sure how to mock ObjectMapper().readValue in that class. I'm fairly new to mockito and could figure out how to do it.

以下是我的代码,

private configDetail fetchConfigDetail(String configId) throws IOException {
    final String response = restTemplate.getForObject(config.getUrl(), String.class);
    return new ObjectMapper().readValue(response, ConfigDetail.class);
}

ServiceTest.java

@Test
public void testgetConfigDetailReturnsNull() throws Exception {

    restTemplate = Mockito.mock(restTemplate.class);
    Service service = new Service();
    Config config = Mockito.mock(Config.class);
    ObjectMapper objMapper = Mockito.mock(ObjectMapper.class);
            Mockito.doReturn("").when(restTemplate).getForObject(anyString(), eq(String.class));
    Mockito.doReturn(configDetail).when(objMapper).readValue(anyString(),eq(ConfigDetail.class));
    assertEquals(configDetail, service.getConfigDetail("1234"));
}

运行此测试时,我得到以下结果,

I get the following results when I run this test,

com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
 at [Source: (String)""; line: 1, column: 0]

在此处发布ServiceTest.Java

Posting ServiceTest.Java here

@RunWith(MockitoJUnitRunner.class)
public class ConfigServiceTest {

    @Mock
    private ConfigPersistenceService persistenceService;

    @InjectMocks
    private ConfigService configService;

    @Mock
    ConfigDetail configDetail;

    @Mock
    private RestTemplate restTemplate;

    @Mock
    private ObjectMapper objMapper;

    @Mock
    private Config config;

    @Test
    public void testgetConfigDetailReturnsNull() throws Exception {

        ObjectMapper objMapper = Mockito.mock(ObjectMapper.class);
        Mockito.doReturn(ucpConfig).when(persistenceService).findById("1234");

        Mockito.doReturn("").when(restTemplate).getForObject(anyString(), eq(String.class));

        Mockito.when((objMapper).readValue("",ConfigDetail.class)).thenReturn(configDetail);
        assertEquals(ConfigDetail, ConfigService.getConfigDetail("1234"));
    }
}

推荐答案

对于您当前的Service类,很难模拟ObjectMapperObjectMapperfetchConfigDetail方法紧密耦合.

With your current Service class it would be difficult to mock ObjectMapper, ObjectMapper is tightly coupled to fetchConfigDetail method.

您必须按如下所示更改服务类以模拟ObjectMapper.

You have to change your service class as follows to mock ObjectMapper.

@Service
public class MyServiceImpl {

    @Autowired
    private ObjectMapper objectMapper;

    private configDetail fetchConfigDetail(String configId) throws IOException {
        final String response = restTemplate.getForObject(config.getUrl(), String.class);
        return objectMapper.readValue(response, ConfigDetail.class);
    }
}

这是我要做的,而不是在我从外部注入的方法内部创建objectMapper(在这种情况下,objectMapper将由Spring创建)

Here what I did is instead of creating objectMapper inside the method I am injecting that from outside (objectMapper will be created by Spring in this case)

更改服务等级后,可以按以下方式模拟objectMapper.

Once you change your service class, you can mock the objectMapper as follows.

ObjectMapper mockObjectMapper = Mockito.mock(ObjectMapper.class);
Mockito.when(mockObjectMapper.readValue(anyString(), any(ConfigDetail.class)).thenReturn(configDetail);

这篇关于如何使用Mockito模拟ObjectMapper.readValue()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆