单元测试 Retrofit 接口声明的好方法

Good way to unit test Retrofit interface declaration

我有下一个接口声明:

public interface FundaService
{
    @GET( "/feeds/Aanbod.svc/json/{key}" )
    Observable<JsonResponse> queryData( @Path( "key" ) String key, @Query("type" ) String type, @Query( "zo" ) String search, @Query( "page" ) int page, @Query( "pagesize" ) int pageSize );
}

我在 Retrofit 之后使用。什么是测试我在 URL 声明和查询参数中没有出错的优雅方法?

我看到我可以模拟 web 层并使用参数检查 url。

更新

我修改了它:

public interface FundaService
{

    String KEY_PATH_PARAM = "key";
    String FEED_PATH = "/feeds/Aanbod.svc/json/{" + KEY_PATH_PARAM + "}";
    String TYPE_QUERY_PARAM = "type";
    String SEARCH_QUERY_PARAM = "zo";
    String PAGE_QUERY_PARAM = "page";
    String PAGESIZE_QUERY_PARAM = "pagesize";

    @GET( FEED_PATH )
    Observable<JsonResponse> queryData( @Path( KEY_PATH_PARAM ) String key, @Query( TYPE_QUERY_PARAM ) String type,
                                        @Query( SEARCH_QUERY_PARAM ) String search, @Query( PAGE_QUERY_PARAM ) int page,
                                        @Query( PAGESIZE_QUERY_PARAM ) int pageSize );
}

并对其进行部分测试,例如:

public class FundaServiceTest
{
    @Test
    public void PathKeyIsCorrect()
        throws Exception
    {
        assertThat( FundaService.KEY_PATH_PARAM ).isEqualTo( "key" );
    }

    @Test
    public void FeedPathIsCorrect()
        throws Exception
    {
        assertThat( FundaService.FEED_PATH ).isEqualTo( "/feeds/Aanbod.svc/json/{key}" );
    }
}

您可以使用 okhttp 拦截器来检查改造构建的最终请求,而无需使用模拟 http 服务器。它使您有机会更早地检查请求。假设我们要测试以下接口 -

public interface AwesomeApi {
  @GET("/cool/stuff")
  Call<Void> getCoolStuff(@Query(("id"))String id);
}

第一个测试运行“validateEagerly”来验证整个界面。如果您的其他测试用例没有触及所有接口方法,这很有用。第二个测试是您如何验证特定调用是否生成预期 url 的示例。

public class AwesomeApiTest {

  @Test
  public void testValidInterface() {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://www.example.com/")
        .addConverterFactory(GsonConverterFactory.create())
        // Will throw an exception if interface is not valid
        .validateEagerly()
        .build();
    retrofit.create(AwesomeApi.class);
  }

  @Test(expected = NotImplementedException.class)
  public void testCoolStuffRequest() throws Exception {
    OkHttpClient client = new OkHttpClient();
    client.interceptors().add(new Interceptor() {
      @Override
      public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
        final Request request = chain.request();
        // Grab the request from the chain, and test away
        assertEquals("HTTP methods should match", "GET", request.method());
        HttpUrl url = request.httpUrl();
        // Test First query parameter
        assertEquals("first query paramter", "id", url.queryParameterName(0));
        // Or, the whole url at once --
        assertEquals("url ", "http://www.example.com/cool/stuff?id=123", url.toString());
        // The following just ends the test with an expected exception.
        // You could construct a valid Response and return that instead
        // Do not return chain.proceed(), because then your unit test may become
        // subject to the whims of the network
        throw new NotImplementedException();
      }
    });
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://www.example.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();
    AwesomeApi awesomeApi = retrofit.create(AwesomeApi.class);
    awesomeApi.getCoolStuff("123").execute();;
  }
}

这个想法是在浏览retrofit自己的测试时得到的。别人的试验往往是很大的启发!