嗨,这是我的测试方法:
char *
这是我的测试用例:
public String getRequestBuilder(String foo1) {
RequestBuilder RequestBuilder = ClientRequest.authorizationProvider(AuthProviderType.footype);
String locationURI = someclassmethod.getLocationURI(RequestBuilder, foo, foo1);
return locationURI;
}
方法
@Test
public void test_foo() {
when(someotherclass.getLocationURI(Matchers.eq(mockRequestBuilder), Matchers.eq("foo"),
Matchers.eq("foo1"))).thenReturn("locationURI");
assertEquals("locationURI", Properties.getRequestBuilder("foo1"));
}
是API提供的静态方法。我不想为此使用PowerMockito。如果我使用
RequestBuilder RequestBuilder = ClientRequest.authorizationProvider(AuthProviderType.foo);
取代
Matchers.any()
测试用例通过。但是使用Matchers.any并不能为我提供确切的价值。有没有办法解决这个测试用例? Foo是我们从另一个类方法中检索的值。
答案 0 :(得分:0)
您仍然可以使用ArgumentCaptor检查传递的值。
@Test
public void test_foo() {
ArgumentCaptor<RequestBuilder> reqCaptor = ArgumentCaptor.forClass(RequestBuilder.class);
when(someotherclass.getLocationURI(any(RequestBuilder.class), anyString(), anyString())).thenReturn("locationURI");
assertEquals("locationURI", Properties.getRequestBuilder("foo1"));
verify(someotherclass).getLocationURI(reqCaptor.capture(), "foo1", "foo2");
//Here you can check that reqCaptor.getValue() is equals to expected RequestBuilder and fail the test if not.
}
在这种情况下,您不需要使用PowerMock来模拟静态方法。
答案 1 :(得分:0)
As an alternative to the ArgumentCaptor, you can also introduce a factory that creates a RequestBuilder object for you.
Then you can mock that factory; and more importantly: the fact that you need to call some static method on some other class ... is hidden in that factory; and doesn't cause trouble for other parts of your code.