use of retrofit2.Retrofit in project retrofit by square.
the class SimpleXmlConverterFactoryTest method setUp.
@Before
public void setUp() {
Format format = new Format(0, null, new HyphenStyle(), Verbosity.HIGH);
Persister persister = new Persister(format);
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(SimpleXmlConverterFactory.create(persister)).build();
service = retrofit.create(Service.class);
}
use of retrofit2.Retrofit in project retrofit by square.
the class WireConverterFactoryTest method setUp.
@Before
public void setUp() {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(WireConverterFactory.create()).build();
service = retrofit.create(Service.class);
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method requestThrowingBeforeExecuteFailsExecute.
@Test
public void requestThrowingBeforeExecuteFailsExecute() throws IOException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
Service service = retrofit.create(Service.class);
server.enqueue(new MockResponse());
final AtomicInteger writeCount = new AtomicInteger();
Object a = new Object() {
@Override
public String toString() {
writeCount.incrementAndGet();
throw new RuntimeException("Broken!");
}
};
Call<String> call = service.postRequestBody(a);
try {
call.request();
fail();
} catch (RuntimeException e) {
assertThat(e).hasMessage("Broken!");
}
assertThat(writeCount.get()).isEqualTo(1);
try {
call.execute();
fail();
} catch (RuntimeException e) {
assertThat(e).hasMessage("Broken!");
}
assertThat(writeCount.get()).isEqualTo(1);
}
use of retrofit2.Retrofit in project retrofit by square.
the class RequestBuilderTest method buildRequest.
static <T> Request buildRequest(Class<T> cls, Object... args) {
final AtomicReference<Request> requestRef = new AtomicReference<>();
okhttp3.Call.Factory callFactory = new okhttp3.Call.Factory() {
@Override
public okhttp3.Call newCall(Request request) {
requestRef.set(request);
throw new UnsupportedOperationException("Not implemented");
}
};
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(new ToStringConverterFactory()).callFactory(callFactory).build();
Method method = TestingUtils.onlyMethod(cls);
//noinspection unchecked
ServiceMethod<T, Call<T>> serviceMethod = (ServiceMethod<T, Call<T>>) retrofit.loadServiceMethod(method);
Call<T> okHttpCall = new OkHttpCall<>(serviceMethod, args);
Call<T> call = serviceMethod.callAdapter.adapt(okHttpCall);
try {
call.execute();
throw new AssertionError();
} catch (UnsupportedOperationException ignored) {
return requestRef.get();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new AssertionError(e);
}
}
use of retrofit2.Retrofit in project retrofit by square.
the class CallTest method transportProblemAsync.
@Test
public void transportProblemAsync() throws InterruptedException {
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ToStringConverterFactory()).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
final AtomicReference<Throwable> failureRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
example.getString().enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
throw new AssertionError();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
failureRef.set(t);
latch.countDown();
}
});
assertTrue(latch.await(10, SECONDS));
Throwable failure = failureRef.get();
assertThat(failure).isInstanceOf(IOException.class);
}
Aggregations