Created
April 4, 2014 07:52
-
-
Save Coinhunter/9970064 to your computer and use it in GitHub Desktop.
This file contains 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 se.springworks.api.client; | |
import com.squareup.okhttp.OkHttpClient; | |
import java.util.List; | |
import retrofit.Callback; | |
import retrofit.RequestInterceptor; | |
import retrofit.RestAdapter; | |
import retrofit.client.OkClient; | |
import se.springworks.api.authentication.BasicAuth; | |
import se.springworks.api.callback.PositionSamplesCallback; | |
import se.springworks.api.datamodel.PositionSample; | |
/** | |
* Created by Conrad Ljungström on 2014-04-01. | |
*/ | |
public class M2HClient { | |
private RestAdapter restAdapter = null; | |
private IAPIInterface m2hDataStore = null; | |
private String serverurl; | |
private String username; | |
private String password; | |
public M2HClient(String serverurl, String username, String password) { | |
this.serverurl = serverurl; | |
this.username = username; | |
this.password = password; | |
this.init(); | |
} | |
public RestAdapter getAdapter() { | |
return restAdapter; | |
} | |
/** | |
* Instantiates the restAdapter and adds the authorization header | |
* to all requests. | |
*/ | |
public void init() { | |
OkHttpClient okHttpClient = new OkHttpClient(); | |
RequestInterceptor requestInterceptor = new RequestInterceptor() { | |
@Override | |
public void intercept(RequestFacade request) { | |
request.addHeader("Authorization", BasicAuth.getHeader(username, password)); | |
} | |
}; | |
restAdapter = new RestAdapter.Builder() | |
.setEndpoint(serverurl) | |
.setRequestInterceptor(requestInterceptor) | |
.setClient(new OkClient(okHttpClient)) | |
.build(); | |
m2hDataStore = restAdapter.create(IAPIInterface.class); | |
} | |
public void positionSamples(final Callback<List<PositionSample>> callback) { | |
m2hDataStore.positionSamples(PositionSamplesCallback.getCallback(callback)); | |
} | |
public void positionSamples( | |
int limit, | |
final Callback<List<PositionSample>> callback) { | |
m2hDataStore.limitPositionSamples(limit, PositionSamplesCallback.getCallback(callback)); | |
} | |
public void positionSamples( | |
int limit, | |
String from_date, | |
String to_date, | |
final Callback<List<PositionSample>> callback) { | |
m2hDataStore.limitDatedPositionSamples(limit, from_date, to_date, PositionSamplesCallback.getCallback(callback)); | |
} | |
} |
This file contains 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
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import java.util.List; | |
import retrofit.Callback; | |
import se.springworks.api.client.M2HClient; | |
import se.springworks.api.datamodel.PositionSample; | |
import org.mockito.*; | |
import static org.mockito.Mockito.mock; | |
/** | |
* Created by Conrad Ljungström on 2014-04-03. | |
*/ | |
public class PositionSampleCallbackTests { | |
M2HClient mockClient = null; | |
List<PositionSample> samples = null; | |
@Captor | |
private ArgumentCaptor<Callback<List<PositionSample>>> cb; | |
@Before | |
public void setUp() { | |
MockitoAnnotations.initMocks(this); | |
mockClient = mock(M2HClient.class); | |
samples = mock(List.class); | |
PositionSample sample1 = new PositionSample(); | |
sample1.setClient_id("client1"); | |
sample1.setLatitude(1); | |
sample1.setLongitude(1); | |
PositionSample sample2 = new PositionSample(); | |
sample2.setClient_id("client2"); | |
sample2.setLatitude(2); | |
sample2.setLongitude(2); | |
PositionSample sample3 = new PositionSample(); | |
sample3.setClient_id("client3"); | |
sample3.setLatitude(3); | |
sample3.setLongitude(3); | |
samples.add(sample1); | |
samples.add(sample2); | |
samples.add(sample3); | |
} | |
@After | |
public void cleanUp() { | |
} | |
@Test | |
public void testPositionSampleCallback() { | |
Mockito.verify(mockClient).positionSamples(cb.capture()); | |
cb.getValue().success(samples, null); | |
/* | |
What do I assert here? In your example you use a (mocked?) listAdapter | |
to check length of but since the only class I instantiate from | |
this library is M2HClient I can't actually check this against anything. | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are you trying to test that when you call positionSamples() that it calls the API? If so, mock m2hDataStore and verify it calls the API method. You'll need some way to swap in the mocked data store into your real m2hclient
Right now your test will not do anything useful because you only have mocks. No "real code" is getting run.Don't mock m2hclient if that is what you are trying to test.