
retrofit2.mock.Calls Maven / Gradle / Ivy
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit2.mock;
import java.io.IOException;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/** Factory methods for creating {@link Call} instances which immediately respond or fail. */
public final class Calls {
public static Call response(T successValue) {
return response(Response.success(successValue));
}
public static Call response(final Response response) {
return new Call() {
@Override public Response execute() throws IOException {
return response;
}
@Override public void enqueue(Callback callback) {
callback.onResponse(this, response);
}
@Override public boolean isExecuted() {
return false;
}
@Override public void cancel() {
}
@Override public boolean isCanceled() {
return false;
}
@SuppressWarnings("CloneDoesntCallSuperClone") // Immutable object.
@Override public Call clone() {
return this;
}
@Override public Request request() {
return response.raw().request();
}
};
}
public static Call failure(final IOException failure) {
return new Call() {
@Override public Response execute() throws IOException {
throw failure;
}
@Override public void enqueue(Callback callback) {
callback.onFailure(this, failure);
}
@Override public boolean isExecuted() {
return false;
}
@Override public void cancel() {
}
@Override public boolean isCanceled() {
return false;
}
@SuppressWarnings("CloneDoesntCallSuperClone") // Immutable object.
@Override public Call clone() {
return this;
}
@Override public Request request() {
return new Request.Builder().url("http://localhost").build();
}
};
}
private Calls() {
throw new AssertionError("No instances.");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy