简体   繁体   English

Mockito 之间的区别。<Response> any().getClass() 和 Mockito。 <Class<Response> &gt; 任何()?

[英]Difference between Mockito.<Response>any().getClass() and Mockito.<Class<Response>>any()?

I am trying to understanding how the generics work in case of Mockito.我试图了解泛型在 Mockito 的情况下是如何工作的。

I am trying to mock the RestTemplate , however the following code does not compile and I don't know why:我正在尝试模拟RestTemplate ,但是以下代码无法编译,我不知道为什么:

var expectedResponse = createServiceResponse();

when(restTemplate.exchange(anyString(), any(HttpMethod.class),
        Mockito.<HttpEntity<Object>>any(),
        Mockito.<ServiceResponse>any().getClass(), anyMap()))
        .thenReturn(ResponseEntity.ok(expectedResponse));

The compiler keeps giving me the error message:编译器不断给我错误信息:

Cannot resolve method 'thenReturn(org.springframework.http.ResponseEntity<T>)

But when I switch Mockito.<ServiceResponse>any().getClass() with Mockito.<Class<ServiceResponse>>any() the compilation succeeds.但是当我将Mockito.<ServiceResponse>any().getClass()Mockito.<Class<ServiceResponse>>any()切换时,编译成功。

Why is this happening ?为什么会这样?

What is the difference between Mockito.<ServiceResponse>any().getClass() and Mockito.<Class<ServiceResponse>>any() ? Mockito.<ServiceResponse>any().getClass()Mockito.<Class<ServiceResponse>>any()什么Mockito.<Class<ServiceResponse>>any()

They should be the same thing right ?他们应该是一回事吧?

I mean the class of ServiceResponse should be Class<ServiceResponse> .我的意思是ServiceResponse的类应该是Class<ServiceResponse> Right ?对 ?

I don't have a good grasp of advanced generics so I can't understand what the compiler is doing.我没有很好地掌握高级泛型,所以我无法理解编译器在做什么。

Please do add some description in the answer so that I can understand why this is working the way it is suppose to.请在答案中添加一些描述,以便我能够理解为什么它会按照预期的方式工作。

What is the difference between Mockito.<ServiceResponse>any().getClass() and Mockito.<Class<ServiceResponse>>any()? Mockito.<ServiceResponse>any().getClass()Mockito.<Class<ServiceResponse>>any()?

Mockito.<ServiceResponse>any() is an instance of ServiceResponse . Mockito.<ServiceResponse>any()ServiceResponse一个实例。 And calling getClass() from any instance return Class<?> .并从任何实例调用getClass()返回Class<?> getClass() is created to get the actual class of the instance, usually for runtime comparison.创建getClass()以获取实例的实际类,通常用于运行时比较。

On the other hand, Mockito.<Class<ServiceResponse>>any() is an instance of Class<ServiceResponse>另一方面, Mockito.<Class<ServiceResponse>>any()Class<ServiceResponse>一个实例

I mean the class of ServiceResponse should be Class.我的意思是 ServiceResponse 的类应该是 Class。 Right ?对 ?

Not really, consider following code不是真的,请考虑以下代码

    private void dummy(ServiceResponse response) {
        System.out.println(response.getClass().equals(ServiceResponse.class));
    }

You can't guarantee the output is always true, as response can be of type CustomServiceResponse (extending ServiceResponse)您不能保证输出始终为真,因为response可以是CustomServiceResponse类型(扩展 ServiceResponse)

Further reading:进一步阅读:
What is the difference between a.getClass() and A.class in Java? Java 中的 a.getClass() 和 A.class 有什么区别? Why does a wildcard on a generic parameter require an explicit cast? 为什么泛型参数上的通配符需要显式转换?
Java: getClass() of bounded type Java:有界类型的 getClass()

Only in these cases, where the compiler cannot garantuee the returned type of the method (here restTemplate.exchange ) we can use the non-type-safe equivalent syntax:只有在这些情况下,编译器不能保证方法的返回类型(这里是restTemplate.exchange ),我们可以使用非类型安全的等效语法:

Mockito.doReturn(ResponseEntity.ok(expectedResponse))
    .when(restTemplate)
    .exchange(anyString(), any(HttpMethod.class),
        Mockito.<HttpEntity<Object>>any(),
        Mockito.<ServiceResponse>any().getClass(), anyMap()));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM