Android unit testing is a good way to transform the interface declaration

I have the next 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 );
}

I used it after renovation. If I didn't make mistakes in URL declaration and query parameters, would it be an elegant test method?

I see that you can simulate the web tier and check the URL with parameters

to update

I modified it:

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

And conduct some tests on it, such as:

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

resolvent:

You can use the okhttp interceptor to check the final request built by the transformation without using a simulated HTTP server. It gives you the opportunity to check the request in advance. Suppose we want to test the following interface-

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

The first test runs "validateeagerly" to verify the entire interface. It is useful if your other test cases do not touch all interface methods. The second test is an example of how to verify whether a specific call is generating the expected 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();;
  }
}

I got this idea from the test of browsing transformation. Other people's tests are usually very enlightening!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>