Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active September 25, 2016 11:53
Show Gist options
  • Save dherges/1972b11026c5b3b5fd0377974c10ecca to your computer and use it in GitHub Desktop.
Save dherges/1972b11026c5b3b5fd0377974c10ecca to your computer and use it in GitHub Desktop.
ok-testing-reloaded-medium
public final class MyAssertions {
public static RecordedRequestAssert assertThat(RecordedRequest recordedRequest) {
return new RecordedRequestAssert(recordedRequest);
}
public static ResponseAssert assertThat(Response<?> response) {
return new ResponseAssert(response);
}
}
import okhttp3.mockwebserver.RecordedRequest;
import org.assertj.core.api.*;
import org.assertj.core.internal.Objects;
import org.assertj.core.internal.Strings;
public class RecordedRequestAssert
extends AbstractAssert<RecordedRequestAssert, RecordedRequest>
implements Assert<RecordedRequestAssert, RecordedRequest> {
Strings strings = Strings.instance();
Objects objects = Objects.instance();
public RecordedRequestAssert(RecordedRequest actual) {
super(actual, RecordedRequestAssert.class);
}
public RecordedRequestAssert isHttp(String verb, String path) {
objects.assertNotNull(info, actual);
strings.assertEqualsIgnoringCase(info, actual.getMethod(), verb);
strings.assertEqualsIgnoringCase(info, actual.getPath(), path);
return this;
}
}
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assert;
import org.assertj.core.internal.Integers;
import org.assertj.core.internal.Objects;
import retrofit2.Response;
public class ResponseAssert
extends AbstractAssert<ResponseAssert, Response<?>>
implements Assert<ResponseAssert, Response<?>> {
Objects objects = Objects.instance();
Integers integers = Integers.instance();
public ResponseAssert(Response<?> actual) {
super(actual, ResponseAssert.class);
}
public ResponseAssert isStatusCode(int statusCode) {
objects.assertNotNull(info, actual);
integers.assertEqual(info, actual.code(), statusCode);
return this;
}
public ResponseAssert isOk() {
return isStatusCode(200);
}
public ResponseAssert hasBody() {
objects.assertNotNull(info, actual);
objects.assertNotNull(info, actual.body());
return this;
}
}
import static ext.assertj.MyAssertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
public class TwitterApiTest {
@Test
public void homeTimeline() throws IOException, InterruptedException {
// replay scripted http response from yaml file
server.enqueue("twitter/statuses/home_timeline");
// execute request
final Response<List<Tweet>> response = twitterApi()
.homeTimeline()
.execute();
// assert response
assertThat(response).isOk().hasBody();
// verify request
assertThat(server.takeRequest()).isHttp("GET", "/statuses/home_timeline.json");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment