如何正确模拟 UriInfo?
how properly mock UriInfo?
那是我的一段代码:
import java.net.URI;
import javax.ws.rs.core.UriInfo;
(...)
UriInfo mockUriInfo;
String url = "test";
mockUriInfo = mock(UriInfo.class);
when(mockUriInfo.getRequestUri()).then(new URI(url));
不幸的是我有一个错误:
then(org.mockito.stubbing.Answer) cannot be applied to (java.new URI)
知道我该如何解决吗?
你需要使用 thenReturn 而不是 then:
when(mockUriInfo.getRequestUri()).thenReturn(new URI(url));
如果您想使用 then(thenAnswer 的同义词,您需要将答案作为参数传递:
when(mockUriInfo.getRequestUri()).then(new Answer<Integer>() {
public URI answer(InvocationOnMock invocation) throws Throwable {
return new URI(url);
}
}
真的 99% 的时候都是错的。相反,最好使用真实对象或使用集成测试(使用 RESTAssured 或其他方式)。
在 mockito wiki 上:Don't mock types you don't own !
This is not a hard line, but crossing this line may have repercussions! (it most likely will.)
- Imagine code that mocks a third party lib. After a particular upgrade of a third library, the logic might change a bit, but the test suite will execute just fine, because it's mocked. So later on, thinking everything is good to go, the build-wall is green after all, the software is deployed and... Boom
- It may be a sign that the current design is not decoupled enough from this third party library.
- Also another issue is that the third party lib might be complex and require a lot of mocks to even work properly. That leads to overly specified tests and complex fixtures, which in itself compromises the compact and readable goal. Or to tests which do not cover the code enough, because of the complexity to mock the external system.
Instead, the most common way is to create wrappers around the external lib/system, though one should be aware of the risk of abstraction leakage, where too much low level API, concepts or exceptions, goes beyond the boundary of the wrapper. In order to verify integration with the third party library, write integration tests, and make them as compact and readable as possible as well.
那是我的一段代码:
import java.net.URI;
import javax.ws.rs.core.UriInfo;
(...)
UriInfo mockUriInfo;
String url = "test";
mockUriInfo = mock(UriInfo.class);
when(mockUriInfo.getRequestUri()).then(new URI(url));
不幸的是我有一个错误:
then(org.mockito.stubbing.Answer) cannot be applied to (java.new URI)
知道我该如何解决吗?
你需要使用 thenReturn 而不是 then:
when(mockUriInfo.getRequestUri()).thenReturn(new URI(url));
如果您想使用 then(thenAnswer 的同义词,您需要将答案作为参数传递:
when(mockUriInfo.getRequestUri()).then(new Answer<Integer>() {
public URI answer(InvocationOnMock invocation) throws Throwable {
return new URI(url);
}
}
真的 99% 的时候都是错的。相反,最好使用真实对象或使用集成测试(使用 RESTAssured 或其他方式)。
在 mockito wiki 上:Don't mock types you don't own !
This is not a hard line, but crossing this line may have repercussions! (it most likely will.)
- Imagine code that mocks a third party lib. After a particular upgrade of a third library, the logic might change a bit, but the test suite will execute just fine, because it's mocked. So later on, thinking everything is good to go, the build-wall is green after all, the software is deployed and... Boom
- It may be a sign that the current design is not decoupled enough from this third party library.
- Also another issue is that the third party lib might be complex and require a lot of mocks to even work properly. That leads to overly specified tests and complex fixtures, which in itself compromises the compact and readable goal. Or to tests which do not cover the code enough, because of the complexity to mock the external system.
Instead, the most common way is to create wrappers around the external lib/system, though one should be aware of the risk of abstraction leakage, where too much low level API, concepts or exceptions, goes beyond the boundary of the wrapper. In order to verify integration with the third party library, write integration tests, and make them as compact and readable as possible as well.