- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试模拟 Spring Rest 的 restTemplate.exchange
方法。
在同一测试中,我有多个调用,仅返回类型不同。
这是我创建的模拟方法
首先
// Original method
restTemplate.exchange(UrlMap.SEARCH + '?' + searchDocsForm.toQueryParams(),
HttpMethod.GET, null, new ParameterizedTypeReference<SearchResultsDTO<SolrDocumentDTO>>() {
})
// Mock
when(restTemplate.exchange(any(String.class), any(HttpMethod.class), any(), Matchers.<ParameterizedTypeReference<SearchResultsDTO<SolrDocumentDTO>>>any())).thenReturn(
new ResponseEntity<>(searchResultsDTO, HttpStatus.OK));
第二
// Original method
restTemplate.exchange(UrlMap.ALL_DOCUS_TOPICS,
HttpMethod.GET, null, new ParameterizedTypeReference<List<SelectItem>>() {
}).getBody();
// Mock
when(restTemplate.exchange(any(String.class), any(HttpMethod.class), any(), Matchers.<ParameterizedTypeReference<List<SelectItem>>>any())).thenReturn(
new ResponseEntity<>(selectItems, HttpStatus.OK));
mock 不考虑 ParameterizedTypeReference
的通用参数,最后定义的 mock 胜过前者。
有什么办法可以让它发挥作用吗?
最佳答案
Mockito 本身并不擅长匹配泛型,但您的解决方案比一般情况要简单得多。
替换你的:
Matchers.<ParameterizedTypeReference<SearchResultsDTO<SolrDocumentDTO>>>any())
与:
eq(new ParameterizedTypeReference<SearchResultsDTO<SolrDocumentDTO>>() {}))
首先,Matchers.any()
不匹配类型,甚至在其 any(Foo.class)
中也不匹配多样性(从 Mockito 1.x 开始)。 any()
火柴all values, including null and including incorrect types :
Matches any object, including nulls
This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.
(“ future 主要版本”可能会在 Mockito 2 中实现,其中可能会引入 isA
样式的类型检查。;参见 commentary from Mockito committer Brice。)
泛型有助于为 exchange
获取正确的参数和 thenReturn
, 但因为 type erasure这些类型信息都没有进入 CLASS 文件,更不用说 JVM 了。唯一断言其参数类型的匹配器是 isA
,它采用类文字并且不会帮助您进行参数化类型。
您可以编写一个自定义匹配器来检查参数的类型和类型参数(如果它们不受删除影响),但对于您的特定情况,这不是必需的。
类型删除是全部原因ParameterizedTypeReference存在:它将泛型信息捕获到子类中,其中参数化类型不会被删除。同样的模式用于 TypeToken在 Guava 或TypeLiteral在吉斯。所有这些实现都将参数化类型描述为实例。
重要的是,所有这些——包括 ParameterizedTypeReference —支持equals
和 hashCode
, 所以 new ParameterizedTypeReference<A<B>>(){}
等于 new ParameterizedTypeReference<A<B>>(){}
即使实例不同。 (See the code here.)
因为对相同参数化类型的引用是相等的,所以使用 Mockito 的 eq
具有不同引用的匹配器,一切都应该没问题。
关于java - 将泛型与 Mockito 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670463/
我一直面临一个奇怪的问题。基本上,当我正常运行 Mockito 测试时,即“作为 Junit 测试运行”时,它给了我以下错误。有人可以帮助我请问我的错误是什么? 收到的错误: java.lan
我正在使用 Mockito 以及 mockito-inline用于模拟静态方法。我正在尝试申请 doNothing或类似的行为,到静态 void 方法。以下解决方法有效,但我认为应该有一种更方便的方法
我正在尝试验证我正在测试的类是否调用了正确的依赖类的方法。所以我试图匹配方法参数,但我并不真正关心这个测试中的实际值,因为我不想让我的测试变得脆弱。 但是,我在设置它时遇到了麻烦,因为 Mockito
我正在使用 Mockito 编写单元测试,并且在模拟注入(inject)的类时遇到问题。问题是两个注入(inject)的类是相同的类型,仅通过它们的 @Qualifier 注释进行区分。如果我尝试简单
在我的断言中的以下简单练习中,我期望 1,但得到 0。为什么我会看到这种行为? public class MockitoTest { POJO mockedPojo; @Before
我正在创建一个通用模拟客户端来测试 HTTP 交互。为此,我希望能够以相同的方法进行多次响应。使用普通模拟,这不是问题: when(mock.execute(any(), any(), any()))
我需要全局模拟类方法。 我的意思是,我不能创建模拟对象和 stub 方法。我的 api 不将此对象作为参数,所以我不能在函数调用中传递它,但是这个类的对象是在这些函数中创建并在那里使用的。这就是为什么
我正在尝试使用 Mockito 2.18.3 框架模拟我们公司内部库中提供的 final 类,不幸的是我们无权更改库中的代码。但每当我运行时,我都会收到以下错误: java.lang.NoClassD
研究了mockito测试框架,学习了powermock,突然发现一个叫powermockito的框架,看不懂了。 谁能告诉我这三个测试工具的区别? 最佳答案 Mockito 是市场标准模拟框架,味道非
我想跳过检查验证调用中的参数之一。因此对于: def allowMockitoVerify=Mockito.verify(msg,atLeastOnce()).handle(1st param,,3r
为了模拟在被测方法内部构造的本地对象上的局部变量/方法调用,我们目前使用的是 PowerMockito 库。 我们正在尝试评估是否可以使用 mockito-inline(版本 3.7.7)来做同样的事
我在想, 如果在 @Before 方法中我正在初始化模拟对象,我不应该在 @After 中取消对它的引用吗?或者那会是多余的吗?为什么? 最佳答案 不需要,JUnit 会为每个测试方法创建一个新的测试
我想使用 Mockito 验证字符串参数是否满足两个条件: verify(mockClass).doSomething(Matchers.startsWith("prefix")); verify(m
如果我像这样创建一个模拟 when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(servicesTestE
使用 Mockito 我遇到了以下问题: Mockito.when(restOperationMock.exchange( Mockito.anyString(), M
我想知道描述中的事情是否可行以及如何去做。 我知道你可以调用原始方法然后像这样做答案: when(presenter, "myMethod").doAnswer() 但我想对它们进行不同的排序,首先执
我试图弄清楚org.mockito.AdditionalMatchers是如何工作的,但我失败了。为什么这个测试失败了? import static org.hamcrest.CoreMatchers
有人知道使用 Mockito 为 ATG 编写单元测试用例吗?我在凝视时遇到了以下讨论 - Automated unit tests for ATG development和 Using PowerM
我想知道描述中的事情是否可行以及如何去做。 我知道你可以调用原始方法然后像这样做答案: when(presenter, "myMethod").doAnswer() 但我想对它们进行不同的排序,首先执
我有以下接口(interface)CatalogVersionService,它公开了一些服务。我还有一个单元测试,它通过使用 Mockito 来模拟这个接口(interface),如下所示: Cat
我是一名优秀的程序员,十分优秀!