我使用 Spring Android Rest Template 来执行 HTTP 请求。我是这样测试的:
package com.mnubo.platform.android.sdk.internal.user.services.impl;
import com.mnubo.platform.android.sdk.models.users.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.client.MockRestServiceServer;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
@RunWith(RobolectricTestRunner.class)
@Config(emulateSdk = 18)
public class DummyTest {
private MyApiImpl myApi;
private MockRestServiceServer myapiMockServer;
@Before
public void setUp() throws Exception {
myApi = new MyApiImpl("ACCESS_TOKEN");//break here
myapiMockServer = MockRestServiceServer.createServer(myApi.getRestTemplate());
}
@Test
public void testRestClient() throws Exception {
final User expectedUser = new User();
expectedUser.setUsername("test");
expectedUser.setLastname("lastname");
myapiMockServer.expect(requestTo("http://my-service.com/user/test"))
.andExpect(method(GET))
.andRespond(withSuccess(this.convertToJsonString(expectedUser), APPLICATION_JSON_UTF8));
User user = myApi.userOperations().getUser("test");
userMockServer.verify();
}
}
所有这些都使用 RoboelectricTestRunner 正常工作。但是,昨天 Android Studio 更新并要求我将构建工具版本更新为 1.1.0。(com.android.tools.build:gradle:1.1.0)。
此版本现在包括对单元测试的支持。请参阅https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support
问题:我不能再创建MyApiImpl
了,因为它创建了一个 RestTemplate。这个 RestTemplate 使用 org.springframework.http.client.HttpComponentsClientHttpRequestFactory 并且在这个类的构造函数中,使用了来自 org.apache.http 包的方法。
这些方法引发了异常:例如:Method getSocketFactory in org.apache.http.conn.scheme.PlainSocketFactory
好吧,我使用 PowerMock 成功地模拟了 getSocketFactory 但随后我不得不模拟只能从构造函数( source)register
本地访问的 SchemeRegistry 方法。
所以我放弃了尝试模拟 RestTemplate 内部发生的所有恶作剧,并决定直接模拟 RestTemplate。
我需要帮助来正确地模拟它,以便MockRestServiceServer
仍然可以在我的测试中使用,因为现在,myapiMockServer.verify()
断言失败了。
更新
我仍然无法使用来测试我的 Api,但我设法使用类似的模拟MockRestServiceServer
来测试每个人:OperationImpl
RestTemplate
public class DummyOperationTest {
private DummyOperation dummyOperation;
private RestTemplate mockedRestTemplate = mock(RestTemplate.class);
@Before
public void setUp() throws Exception {
super.setUp();
dummyOperation = new DummyOperationImpl(PLATFORM_BASE_URL, mockedRestTemplate);
}
@Test
public void test() throws Exception {
String calledUrl = PLATFORM_BASE_URL + "/objects?update_if_exists=true";
when(mockedRestTemplate.postForObject(calledUrl, expectedSmartObject, SmartObject.class)).thenReturn(expectedSmartObject);
smartObjectService.create(expectedSmartObject, true);
verify(mockedRestTemplate, atMost(1)).postForObject(calledUrl, expectedSmartObject, SmartObject.class);
}
}
不幸的是,这仍然不能测试整个请求的执行。我无法验证 oAuth 身份验证是否已正确添加到标头中,或者服务器响应的转换是否正确。