use of retrofit2.Converter in project retrofit by square.
the class FallbackParserFinderTest method converterFactoryFallsBackToParserField.
@Test
public void converterFactoryFallsBackToParserField() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://localhost/").build();
ProtoConverterFactory factory = ProtoConverterFactory.create();
Converter<ResponseBody, ?> converter = factory.responseBodyConverter(FakePhone.class, new Annotation[0], retrofit);
assertThat(converter).isNotNull();
}
use of retrofit2.Converter in project retrofit by square.
the class RetrofitTest method responseConverterFactoryNoMatchThrows.
@Test
public void responseConverterFactoryNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingConverterFactory nonMatchingFactory = new NonMatchingConverterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory).build();
try {
retrofit.responseBodyConverter(type, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + " Tried:\n" + " * retrofit2.BuiltInConverters\n" + " * retrofit2.helpers.NonMatchingConverterFactory");
}
assertThat(nonMatchingFactory.called).isTrue();
}
use of retrofit2.Converter in project retrofit by square.
the class RetrofitTest method methodAndParameterAnnotationsPassedToRequestBodyConverter.
@Test
public void methodAndParameterAnnotationsPassedToRequestBodyConverter() {
final AtomicReference<Annotation[]> parameterAnnotationsRef = new AtomicReference<>();
final AtomicReference<Annotation[]> methodAnnotationsRef = new AtomicReference<>();
class MyConverterFactory extends Converter.Factory {
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
parameterAnnotationsRef.set(parameterAnnotations);
methodAnnotationsRef.set(methodAnnotations);
return new ToStringConverterFactory().requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
Annotated annotated = retrofit.create(Annotated.class);
// Trigger internal setup.
annotated.bodyParameter(null);
assertThat(parameterAnnotationsRef.get()).hasAtLeastOneElementOfType(Annotated.Foo.class);
assertThat(methodAnnotationsRef.get()).hasAtLeastOneElementOfType(POST.class);
}
use of retrofit2.Converter in project retrofit by square.
the class RetrofitTest method stringConverterCalledForString.
@Test
public void stringConverterCalledForString() {
final AtomicBoolean factoryCalled = new AtomicBoolean();
class MyConverterFactory extends Converter.Factory {
@Override
public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
factoryCalled.set(true);
return null;
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
CallMethod service = retrofit.create(CallMethod.class);
Call<ResponseBody> call = service.queryString(null);
assertThat(call).isNotNull();
assertThat(factoryCalled.get()).isTrue();
}
use of retrofit2.Converter in project retrofit by square.
the class RetrofitTest method responseConverterFactorySkippedNoMatchThrows.
@Test
public void responseConverterFactorySkippedNoMatchThrows() {
Type type = String.class;
Annotation[] annotations = new Annotation[0];
NonMatchingConverterFactory nonMatchingFactory1 = new NonMatchingConverterFactory();
NonMatchingConverterFactory nonMatchingFactory2 = new NonMatchingConverterFactory();
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory1).addConverterFactory(nonMatchingFactory2).build();
try {
retrofit.nextResponseBodyConverter(nonMatchingFactory1, type, annotations);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + " Skipped:\n" + " * retrofit2.BuiltInConverters\n" + " * retrofit2.helpers.NonMatchingConverterFactory\n" + " Tried:\n" + " * retrofit2.helpers.NonMatchingConverterFactory");
}
assertThat(nonMatchingFactory1.called).isFalse();
assertThat(nonMatchingFactory2.called).isTrue();
}
Aggregations