Java 如何模拟API的静态方法
嗨,这是我的测试方法:Java 如何模拟API的静态方法,java,api,testing,junit,Java,Api,Testing,Junit,嗨,这是我的测试方法: public String getRequestBuilder(String foo1) { RequestBuilder RequestBuilder = ClientRequest.authorizationProvider(AuthProviderType.footype); String locationURI = someclassmethod.getLocationURI(RequestBuilder, foo, foo1); retu
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"));
}
在方法中
RequestBuilder RequestBuilder = ClientRequest.authorizationProvider(AuthProviderType.foo);
是API提供的静态方法。我不想为此使用PowerMockito。如果我使用
Matchers.any()
代替
Matchers.eq(mockRequestBuilder)
测试用例通过了。但是使用Matchers.any无法提供确切的值。有没有办法进入这个测试用例?Foo是我们从另一个类方法检索的值。您仍然可以使用检查传递的值
@测试
公开无效测试_foo(){
ArgumentCaptor reqCaptor=ArgumentCaptor.forClass(RequestBuilder.class);
当(someotherclass.getLocationURI(any(RequestBuilder.class)、anyString()、anyString())。然后返回(“locationURI”);
assertEquals(“locationURI”,Properties.getRequestBuilder(“foo1”);
验证(someotherclass).getLocationURI(reqCaptor.capture(),“foo1”,“foo2”);
//在这里,您可以检查reqCaptor.getValue()是否等于预期的RequestBuilder,如果不是,则测试失败。
}
在这种情况下,您不需要使用PowerMock-to-mock静态方法。您仍然可以使用检查传递的值
@测试
公开无效测试_foo(){
ArgumentCaptor reqCaptor=ArgumentCaptor.forClass(RequestBuilder.class);
当(someotherclass.getLocationURI(any(RequestBuilder.class)、anyString()、anyString())。然后返回(“locationURI”);
assertEquals(“locationURI”,Properties.getRequestBuilder(“foo1”);
验证(someotherclass).getLocationURI(reqCaptor.capture(),“foo1”,“foo2”);
//在这里,您可以检查reqCaptor.getValue()是否等于预期的RequestBuilder,如果不是,则测试失败。
}
在这种情况下,您不需要使用PowerMock-to-mock静态方法。作为ArgumentCaptor的替代方法,您还可以引入一个工厂来为您创建RequestBuilder对象
然后你可以嘲笑那家工厂;更重要的是:您需要在其他类上调用一些静态方法。。。藏在那家工厂里;并且不会给代码的其他部分带来麻烦。作为ArgumentCaptor的替代方案,您还可以引入一个工厂,为您创建RequestBuilder对象
然后你可以嘲笑那家工厂;更重要的是:您需要在其他类上调用一些静态方法。。。藏在那家工厂里;并且不会给代码的其他部分带来麻烦。此API是否有任何方法来配置授权提供程序返回的确切内容?可能是配置属性或依赖项注入机制。此API是否有任何方法来配置授权提供程序所返回的内容?可能是配置属性或依赖项注入机制。在检查RequestBuilder argumentCaptor的实际值时,我是否应该调用:someotherclass.getLocationURI(Matchers.any(RequestBuilder.class)、Matchers.eq(“foo”)、Matchers.eq(“foo1”);?传递Matchers.any(RequestBuilder.class)的值,我想您应该测试
getRequestBuilder()
方法。因此,getLocationURI
必须在getRequestBuilder
内部调用。无需显式调用getLocationURI
。但是RequestBuilder是使用静态方法构建的,然后作为参数发送到getLocationURI方法。是的,但不能使用Mockito
模拟静态方法。所以,如果模拟它对测试非常重要,并且您不想使用PowerMock
,我建议重构您的方法,使其更易于测试。在测试中使用Matchers.any()
,然后使用ArgumentCaptor
检查传递给getLocationURI的值,同时检查RequestBuilder ArgumentCaptor的实际值,我是否应该调用:someotherclass.getLocationURI(Matchers.any(RequestBuilder.class))Matchers.eq(“foo”),Matchers.eq(“foo1”);?传递Matchers.any(RequestBuilder.class)的值,我想您应该测试getRequestBuilder()
方法。因此,getLocationURI
必须在getRequestBuilder
内部调用。无需显式调用getLocationURI
。但是RequestBuilder是使用静态方法构建的,然后作为参数发送到getLocationURI方法。是的,但不能使用Mockito
模拟静态方法。所以,如果模拟它对测试非常重要,并且您不想使用PowerMock
,我建议重构您的方法,使其更易于测试。只要在测试中使用Matchers.any()
,然后使用ArgumentCaptor
检查传递给getLocationURI
@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.
}