Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单元测试改型接口声明的好方法_Android_Unit Testing_Retrofit - Fatal编程技术网

Android 单元测试改型接口声明的好方法

Android 单元测试改型接口声明的好方法,android,unit-testing,retrofit,Android,Unit Testing,Retrofit,我有下一个接口声明: 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, @Quer

我有下一个接口声明:

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 );
}
并对其进行部分测试,如:

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

我是通过浏览改造公司自己的测试得出这个想法的。别人的测试往往是很大的鼓舞

谢谢!我今天要试试,我会接受答案的
public interface AwesomeApi {
  @GET("/cool/stuff")
  Call<Void> getCoolStuff(@Query(("id"))String id);
}
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();;
  }
}