Created
March 17, 2015 14:31
-
-
Save daddykotex/e8637dcc7dfa0fffb9b5 to your computer and use it in GitHub Desktop.
Test your Spring Rest Template APIs with the new Android Unit testing that requires mocking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mnubo.platform.android.sdk.internal; | |
import android.util.Log; | |
import org.apache.http.conn.params.ConnManagerParams; | |
import org.apache.http.conn.scheme.PlainSocketFactory; | |
import org.apache.http.conn.scheme.SchemeRegistry; | |
import org.apache.http.conn.ssl.SSLSocketFactory; | |
import org.apache.http.impl.client.AbstractHttpClient; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.params.HttpParams; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.powermock.core.classloader.annotations.PrepareForTest; | |
import org.powermock.modules.junit4.PowerMockRunner; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
import org.springframework.test.web.client.MockRestServiceServer; | |
import static org.powermock.api.mockito.PowerMockito.mock; | |
import static org.powermock.api.mockito.PowerMockito.mockStatic; | |
import static org.powermock.api.mockito.PowerMockito.when; | |
import static org.powermock.api.mockito.PowerMockito.whenNew; | |
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content; | |
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; | |
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; | |
@RunWith(PowerMockRunner.class) | |
@PrepareForTest({ | |
Log.class, | |
PlainSocketFactory.class, | |
SSLSocketFactory.class, | |
HttpComponentsClientHttpRequestFactory.class, | |
ConnManagerParams.class, | |
AbstractHttpClient.class | |
}) | |
public class DummyTest { | |
private final String USER_ACCESS_TOKEN = "user_token"; | |
private final PlainSocketFactory mockedPlainSocketFactory = mock(PlainSocketFactory.class); | |
private final SSLSocketFactory mockedSSLSocketFactory = mock(SSLSocketFactory.class); | |
private final SchemeRegistry mockedSchemeRegistry = mock(SchemeRegistry.class); | |
private final DefaultHttpClient mockedHttpClient = mock(DefaultHttpClient.class); | |
private final HttpParams mockedHttpParams = mock(HttpParams.class); | |
private DummyApiImpl dummyApi = new DummyApiImpl(USER_ACCESS_TOKEN); | |
protected MockRestServiceServer mockServer; | |
@Before | |
public void setUp() throws Exception { | |
mockStatic(Log.class); | |
mockStatic(PlainSocketFactory.class); | |
mockStatic(SchemeRegistry.class); | |
mockStatic(SSLSocketFactory.class); | |
mockStatic(ConnManagerParams.class); | |
whenNew(SchemeRegistry.class).withAnyArguments().thenReturn(mockedSchemeRegistry); | |
whenNew(DefaultHttpClient.class).withAnyArguments().thenReturn(mockedHttpClient); | |
when(PlainSocketFactory.getSocketFactory()).thenReturn(mockedPlainSocketFactory); | |
when(SSLSocketFactory.getSocketFactory()).thenReturn(mockedSSLSocketFactory); | |
when(mockedHttpClient.getParams()).thenReturn(mockedHttpParams); | |
mockServer = MockRestServiceServer.createServer(dummyApi.getRestTemplate()); | |
} | |
@Test | |
public void doOperationTest() throws Exception { | |
final User testUser = new User(); | |
mockUserServiceServer.expect(requestTo(expectedUrl("/users/test"))) | |
.andExpect(method(POST)) | |
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | |
.andRespond(withSuccess()); | |
dummyApi.userOperations().createUser("test", testUser); | |
mockUserServiceServer.verify(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment