java Mockito 嘲笑 restTemplate.postForEntity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25272391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:45:12  来源:igfitidea点击:

Mockito mocking restTemplate.postForEntity

javaunit-testingtestingmockingmockito

提问by Dax Durax

I am trying to mock restTemplate.postForEntitymethod,

我正在尝试模拟restTemplate.postForEntity方法,

The actual method call is:

实际的方法调用是:

URI myUri = new URI(myString);
HttpEntity<String> myEntity ...


String myResponse = restTemplate.postForEntity(myUri, myEntity, String.class);

What I have in my test class is:

我在测试课上的内容是:

Mockito.when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), eq(String.class))).thenReturn(response);

This does not work; I have tried several other permutations with no success either. Any suggestions appreciated, thanks.

这不起作用;我尝试了其他几种排列也没有成功。任何建议表示赞赏,谢谢。

By this does not work I mean that the actual method is called and not the mocked method (so the desired result is not returned etc.)

这不起作用我的意思是调用了实际方法而不是模拟方法(因此不会返回所需的结果等)

回答by Abhishek Singh

The following code works for me - when(mockRestTemplate.postForEntity(anyString(), any(), eq(String.class))).thenReturn(response);

以下代码对我有用- when(mockRestTemplate.postForEntity(anyString(), any(), eq(String.class))).thenReturn(response);

回答by Paulo Schreiner

You have to make sure that you initialize the restTemplate as a mock in your tests

您必须确保在测试中将 restTemplate 初始化为模拟

 RestTemplate restTemplate = mock(RestTemplate.class);

AND that this (mocked) rest template is the one being used in your actual method call. You could have a setRestTemplate() method on your object, and you could use that to set the restTemplate:

并且这个(模拟的)rest 模板是您实际方法调用中使用的模板。你可以在你的对象上有一个 setRestTemplate() 方法,你可以使用它来设置 restTemplate:

 myTestObject.setRestTemplate(restTemplate);

Mocks will neverjust call the original method, so if that's happening you can be pretty sure your actual method is not using a mock. (Real) mocks will either return what you told them to, or null.

模拟永远不会只调用原始方法,因此如果发生这种情况,您可以非常确定您的实际方法没有使用模拟。(真实)模拟将返回您告诉他们的内容,或者返回空值。

回答by Larry

I'd guess that the postForEntitymethod is final - you could use RestOperationsinstead of RestTemplateto work around that.

我猜这个postForEntity方法是最终的 - 你可以使用RestOperations而不是RestTemplate解决这个问题。

回答by anayagam

This is what worked for me Firstly a resttemplate needs to be mocked in the test class

这对我有用首先需要在测试类中模拟一个resttemplate

@Mock private RestTemplate mockRestTemplate;

Since ResponseEntity returns an Object create another method that returns the expected response wrapped in ResponseEntity

由于 ResponseEntity 返回一个 Object 创建另一个方法,该方法返回包装在 ResponseEntity 中的预期响应

private ResponseEntity<Object> generateResponseEntityObject(String response, HttpStatus httpStatus){
    return new ResponseEntity<>(response, httpStatus);
}

In your test case, you can now mock the expected response as follows

在您的测试用例中,您现在可以模拟预期的响应如下

String string = "result";
when(mockRestTemplate.postForEntity(anyString(), any(), any()))
        .thenReturn(generateResponseEntityObject(string, HttpStatus.OK));

回答by Pomagranite

resttemplate returns ResponseEntity

resttemplate 返回 ResponseEntity

Postforentityso something like ResponseEntitymyResponse= restTemplate.postForEntity(myUri, myEntity, StringToReturn.class); where StringToReturn is whatever type to return (just String in your case)

Postforentity类似于 ResponseEntitymyResponse=restTemplate.postForEntity(myUri, myEntity, StringToReturn.class); 其中 StringToReturn 是要返回的任何类型(在您的情况下只是 String )