Skip to content

Commit

Permalink
Merge pull request square#342 from square/jw/mock-adapter
Browse files Browse the repository at this point in the history
Add adapter for wrapping mock service implementations to mimic real behavior.
  • Loading branch information
JakeWharton committed Nov 7, 2013
2 parents 5cbe9f2 + 464011a commit d948951
Show file tree
Hide file tree
Showing 11 changed files with 1,176 additions and 47 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<modules>
<module>retrofit</module>
<module>retrofit-converters</module>
<module>retrofit-mock</module>
<module>retrofit-samples</module>
</modules>

Expand Down Expand Up @@ -125,7 +126,7 @@
<artifactId>simple-xml</artifactId>
<version>${simplexml.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
50 changes: 50 additions & 0 deletions retrofit-mock/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup.retrofit</groupId>
<artifactId>parent</artifactId>
<version>1.2.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>retrofit-mock</artifactId>
<name>Retrofit Mock Adapter</name>

<dependencies>
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
37 changes: 37 additions & 0 deletions retrofit-mock/src/main/java/retrofit/MockHttpException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2013 Square, Inc.
package retrofit;

/**
* An exception used to trigger the simulation of an HTTP error for mock services.
*
* @see MockRestAdapter
*/
public class MockHttpException extends RuntimeException {
public final int code;
public final String reason;
public final Object responseBody;

/**
* Create a new HTTP exception.
*
* @param code Corresponding HTTP status code to trigger.
* @param responseBody Object to use as the contents of the response body.
*/
public MockHttpException(int code, Object responseBody) {
this(code, "", responseBody);
}

/**
* Create a new HTTP exception.
*
* @param code HTTP status code to trigger.
* @param reason HTTP status reason message.
* @param responseBody Object to use as the contents of the response body.
*/
public MockHttpException(int code, String reason, Object responseBody) {
super("HTTP " + code + " " + reason);
this.code = code;
this.reason = reason;
this.responseBody = responseBody;
}
}
22 changes: 22 additions & 0 deletions retrofit-mock/src/main/java/retrofit/MockHttpRetrofitError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2013 Square, Inc.
package retrofit;

import java.lang.reflect.Type;
import retrofit.client.Response;

class MockHttpRetrofitError extends RetrofitError {
private final Object body;

MockHttpRetrofitError(String url, Response response, Object body) {
super(url, response, null, null, false, null);
this.body = body;
}

@Override public Object getBody() {
return body;
}

@Override public Object getBodyAs(Type type) {
return body;
}
}
Loading

0 comments on commit d948951

Please sign in to comment.