简体   繁体   English

模拟的restTemplate.postForObject未在测试执行中使用

[英]Mocked restTemplate.postForObject is not being used in execution of test

I have mocked a postForObject resttemplate call using mickito. 我已经使用mickito模拟了一个postForObject resttemplate调用。

Mockito.when(restTemplate.postForObject(Mockito.eq(remoteServerlocation), Mockito.any(Input.class), Mockito.eq(String.class))).thenReturn(responseString); Mockito.when(restTemplate.postForObject(Mockito.eq(remoteServerlocation),Mockito.any(Input.class),Mockito.eq(String.class)))。thenReturn(responseString);

But in the actual code, this mocked value is not being used and trying to call the remote location. 但是在实际代码中,没有使用该模拟值,而是试图调用远程位置。

String responseString = restTemplate.postForObject(url, input, String.class); 字符串responseString = restTemplate.postForObject(url,input,String.class);

As per my understanding , I have mocked exactly same call. 根据我的理解,我嘲笑了完全相同的电话。 But not working. 但是没有用。 Any help on this will be thankful. 在这方面的任何帮助将是感激的。

I'm autowiring the class which contains the testing method in testcase class. 我正在自动装配包含用例类中的测试方法的类。 And this test class I have created restTemplate using new. 这个测试类我已经使用new创建了restTemplate。

TestClass 测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
public class ActionImplTest {

    @Autowired
    private ActionImpl recommendation;

    RestTemplate restTemplate = new RestTemplate();

    @Test
    public void performActionTest() throws Exception {
        String textInput = "InputText";
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("convId", "C123");
        map.put("reID", 1);
        map.put("chID", "Chann_1");

        String convID = "1254356671563";
        String chId = "2";
        String responseString = "Success"
        Mockito.when(restTemplate.postForObject(Mockito.eq("remoteServerlocation"), Mockito.any(Input.class), Mockito.eq(String.class))).thenReturn(responseString);

        Map<String, Object> response = recommendation.performAction(textInput, map, convID, chId);  
    }
}

Add a verify step at the end of your test, mockito will give you some hints on what was expected and what actually has been called: 在测试结束时添加一个verify步骤,mockito将为您提供一些提示,告诉您预期的内容和实际的内容:

Mockito.verify(restTemplate).postForObject(
    Mockito.eq(remoteServerlocation), Mockito.any(Input.class), Mockito.eq(String.class));

Here is an example of what I meant in the comment referring to the Spring documentation . 这是我在引用Spring文档的注释中所指的示例。

@RunWith(SpringRunner.class)
@SpringBootTest
public class ActionImplTest {

    @MockBean
    private RestTemplate restTemplate;

    @Autowired
    private ActionImpl recommendation;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestTemplate.postForObject 和列表 - RestTemplate.postForObject and Lists RestTemplate.postForObject()有效,但是exchange()无效 - RestTemplate.postForObject() works, but exchange() does not Spring 启动 - restTemplate.postForObject - 参数是 null - Spring boot - restTemplate.postForObject - params are null restTemplate.postForObject 没有发送 object 的所有成员变量 - restTemplate.postForObject not sending all the member variables of object 在使用微服务时通过 restTemplate.postForObject() 接收空列表 - Recieving Empty list via restTemplate.postForObject() while consuming a microservice 使用 RestTemplate.postForObject 调用 API 时出现 400 Bad Request - 400 Bad Request when using RestTemplate.postForObject to call API Spring Boot RestTemplate.postForObject 到 Firebase 消息不返回 - Spring Boot RestTemplate.postForObject to Firebase Messaging not returning 返回ResponseEntity和Http错误时,RestTemplate.postForObject返回Null - RestTemplate.postForObject Returns Null When ResponseEntity and Http Error Are Returned RestTemplate.postForObject - 错误:org.springframework.web.client.HttpClientErrorException:400错误请求 - RestTemplate.postForObject - Error: org.springframework.web.client.HttpClientErrorException: 400 Bad Request 在单元测试中模拟RestTemplate#postForObject - Mocking RestTemplate#postForObject in a unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM