以下の例は統合テストにおいてlocalhost:9099/beanmgrに対してGETのリクエストを投げる例.
サービスインターフェース定義
package org.tanuneko;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
/**
* Created by neko32 on 2016/03/06.
*/
public interface WebInterfaceIF {
@GET("tanubean/{id}")
public Call<TanuBean> getNeko(@Path("id") int id);
@POST("tanubean")
public Call<TanuResponse> createNeko(@Body TanuBean bean);
}
統合テストからサービスインタフェース経由でGETリクエストを出す
@Test
public void testTanuAppWithRetroFit() throws IOException {
Retrofit retro = new Retrofit.Builder()
.baseUrl("http://localhost:9099/beanmgr/")
.addConverterFactory(GsonConverterFactory.create())
.build();
WebInterfaceIF service = retro.create(WebInterfaceIF.class);
Call<TanuBean> tanu = service.getNeko(1);
TanuBean t = tanu.execute().body();
System.out.println("Retrived - id:" + t.getId() + ",name:" + t.getName());
assertThat(t.getId(), is(1));
assertThat(t.getName(), is("Tanuchan"));
}