android-单元测试改造接口声明的好方法

我有下一个接口声明:

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 );
}

我在翻新之后使用的.如果我没有在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解析,全网最具深度的三次握手、四次挥手讲解,真香


下一篇:java-使用Spring Security 3.1.3记住我-不建议使用的默认构造函数