|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Square, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package retrofit2; |
| 17 | + |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import okhttp3.mockwebserver.MockResponse; |
| 20 | +import okhttp3.mockwebserver.MockWebServer; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Rule; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.robolectric.RobolectricTestRunner; |
| 26 | +import org.robolectric.annotation.Config; |
| 27 | +import retrofit2.helpers.ToStringConverterFactory; |
| 28 | +import retrofit2.http.GET; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.junit.Assert.fail; |
| 32 | +import static org.robolectric.annotation.Config.NEWEST_SDK; |
| 33 | + |
| 34 | +@RunWith(RobolectricTestRunner.class) |
| 35 | +@Config(sdk = NEWEST_SDK) |
| 36 | +public final class CompletableFutureAndroidTest { |
| 37 | + @Rule public final MockWebServer server = new MockWebServer(); |
| 38 | + |
| 39 | + interface Service { |
| 40 | + @GET("/") CompletableFuture<String> endpoint(); |
| 41 | + } |
| 42 | + |
| 43 | + private Service service; |
| 44 | + |
| 45 | + @Before public void setUp() { |
| 46 | + Retrofit retrofit = new Retrofit.Builder() |
| 47 | + .baseUrl(server.url("/")) |
| 48 | + .addConverterFactory(new ToStringConverterFactory()) |
| 49 | + .build(); |
| 50 | + service = retrofit.create(Service.class); |
| 51 | + } |
| 52 | + |
| 53 | + @Config(sdk = 24) |
| 54 | + @Test public void completableFutureApi24() throws Exception { |
| 55 | + server.enqueue(new MockResponse().setBody("Hi")); |
| 56 | + |
| 57 | + CompletableFuture<String> future = service.endpoint(); |
| 58 | + assertThat(future.get()).isEqualTo("Hi"); |
| 59 | + } |
| 60 | + |
| 61 | + @Config(sdk = 21) |
| 62 | + @Test public void completableFuturePreApi24() { |
| 63 | + try { |
| 64 | + service.endpoint(); |
| 65 | + fail(); |
| 66 | + } catch (IllegalArgumentException e) { |
| 67 | + assertThat(e).hasMessage( |
| 68 | + "Unable to create call adapter for java.util.concurrent.CompletableFuture<java.lang.String>\n" |
| 69 | + + " for method Service.endpoint"); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments