Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MockRetrofit

 MockRetrofit

http://rmp-quipper.connpass.com/event/31744/
MockWebServer and MockRetrofit

Masayuki Izumi

May 27, 2016
Tweet

More Decks by Masayuki Izumi

Other Decks in Programming

Transcript

  1. . P D L 3 F U S P G

    J U .BTBZVLJ*;6.*!J[VNJO LZPCBTIJEFY
  2. > Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at the

    Univ. of Tokyo > Strobo, Inc. / Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter
  3. T R V B S F  S F U

    S P G J U  W   Y // Entity class public class User { public long id; public String name; public String screenName; public Date createdAt; public Date updatedAt; public String icon; public String email; }
  4. T R V B S F  S F U

    S P G J U  W   Y // API client interface public interface UsersApi { @GET("/v1/user") Single<User> get( @Query("access_token") String accessToken ); }
  5. T R V B S F  S F U

    S P G J U  W   Y X J U I  3 Y + B W B  B O E  ( T P O Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.esa.io/") // Uses Gson for response's deserialization .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // Allows to return rx.Observable/rx.Single from service methods .addConverterFactory(GsonConverterFactory.create(gson)) .build(); // Generates an implementation of the UsersApi interface UsersApi api = retrofit.create(UsersApi.class);
  6. T R V B S F  S F U

    S P G J U  W   Y X J U I  3 Y + B W B // Generates an implementation of the UsersApi interface api.get(accessToken) .subscribeOn(Schedulers.io()) .subscribe(user -> { // do something... })
  7. ) P X  U P  U F T

    U  J U ) P X  U P  N P D L  J U
  8.   % F G J O F T 

    B  N P D L  D M B T T  Z P V S T F M G class MockUsersApi implements UsersApi { @Override Single<User> get(String accessToken) { User mockUser = new User(); // ... return mockUser; } } // write your test cases below...
  9.   4 U V C T  N F

    U I P E T  F  H   . P D L J U P UsersApi mockUsersApi = mock(UsersApi.class); when(mockUsersApi.get(accessToken)).thenReturn(mockUser); // write your test cases below...
  10.   6 T F T  . P D

    L 8 F C 4 F S W F S D P N  T R V B S F V Q  P L I U U Q   N P D L X F C T F S W F S MockWebServer server = new MockWebServer(); // Read a mock json file from resources String mockBody = ""; server.enqueue(new MockResponse().setBody(mockBody)); // Start the server server.start();
  11.   6 T F T  . P D

    L 3 F U S P G J U D P N  T R V B S F V Q  S F U S P G J U   S F U S P G J U  N P D L Retrofit retrofit = new Retrofit.Builder() // snip. .build(); NetworkBehavior behavior = NetworkBehavior.create() // if you need MockRetrofit mockRetrofit = new MockRetrofit.Build(retrofit) .networkBehavior(behavior) // if you need .build(); UsersApi usersApi = MockUsersApi(mockRetrofit.create(UsersApi.class))
  12.   6 T F T  . P D

    L 3 F U S P G J U D P N  T R V B S F V Q  S F U S P G J U   S F U S P G J U  N P D L Retrofit retrofit = new Retrofit.Builder() // snip. .build(); NetworkBehavior behavior = NetworkBehavior.create() // if you need MockRetrofit mockRetrofit = new MockRetrofit.Build(retrofit) .networkBehavior(behavior) // if you need .build(); UsersApi usersApi = MockUsersApi(mockRetrofit.create(UsersApi.class))
  13.   6 T F T  . P D

    L 3 F U S P G J U D P N  T R V B S F V Q  S F U S P G J U   S F U S P G J U  N P D L Retrofit retrofit = new Retrofit.Builder() // snip. .build(); NetworkBehavior behavior = NetworkBehavior.create() // if you need MockRetrofit mockRetrofit = new MockRetrofit.Build(retrofit) .networkBehavior(behavior) // if you need .build(); UsersApi usersApi = MockUsersApi(mockRetrofit.create(UsersApi.class))
  14.   6 T F T  . P D

    L 3 F U S P G J U D P N  T R V B S F V Q  S F U S P G J U   S F U S P G J U  N P D L Retrofit retrofit = new Retrofit.Builder() // snip. .build(); NetworkBehavior behavior = NetworkBehavior.create() // if you need MockRetrofit mockRetrofit = new MockRetrofit.Build(retrofit) .networkBehavior(behavior) // if you need .build(); UsersApi usersApi = MockUsersApi(mockRetrofit.create(UsersApi.class))
  15.   S F U S P G J U

      N P D L   # F I B W J P S % F M F H B U F class MockUsersApi implements UsersApi { private final BehaviorDelegate<UsersApi> delegate; // snip the constructor @Override Single<User> get(String accessToken) { User mockUser = new User(); // ... return delegate.returning(Calls.response(mockUser)).fetch(accessToken); } }
  16.   S F U S P G J U

      N P D L   # F I B W J P S % F M F H B U F class MockUsersApi implements UsersApi { private final BehaviorDelegate<UsersApi> delegate; // snip the constructor @Override Single<User> get(String accessToken) { User mockUser = new User(); // ... return delegate.returning(Calls.response(mockUser)).fetch(accessToken); } }
  17.   S F U S P G J U

      N P D L   # F I B W J P S % F M F H B U F class MockUsersApi implements UsersApi { private final BehaviorDelegate<UsersApi> delegate; // snip the constructor @Override Single<User> get(String accessToken) { User mockUser = new User(); // ... return delegate.returning(Calls.response(mockUser)).fetch(accessToken); } } Call<T> response(T) Call<T> response(Response<T>) Call<T> failure(IOException)
  18.   S F U S P G J U

      N P D L   / F U X P S L # F I B W J P S Retrofit retrofit = new Retrofit.Builder() // snip. .build(); NetworkBehavior behavior = NetworkBehavior.create() // if you need MockRetrofit mockRetrofit = new MockRetrofit.Build(retrofit) .networkBehavior(behavior) // if you need .build(); UsersApi usersApi = MockUsersApi(mockRetrofit.create(UsersApi.class))
  19.   S F U S P G J U

      N P D L   / F U X P S L # F I B W J P S NetworkBehavior behavior = NetworkBehavior.create(); // DEFAULT: 2000 ms behavior.setDelay(500, TimeUnit.MILLISECONDS); // DEFAULT: 3 % behavior.setFailurePercent(50); // DEFAULT: 40 % behavior.setVariancePercent(50);
  20. $ P O D M V T J P O

     5PNPDLNFUIPETPGUIFSFUPSpUJOUFSGBDF  JNQMFNFOUTNPDLDMBTTZPVSTFMG  TUVCTNFUIPET FH.PDLJUP  VTFT.PDL8FC4FSWFS  VTFT.PDL3FUSPpU
  21. $ P O D M V T J P O

     .PDL8FC4FSWFS .PDL3FUSPpU TUVCCJOHNFUIPET NPDLDMBTTFT TJNQMF qFYJCMF ˕ ˕ ˓ ˓ ˚ ˚ !4NBMM5FTU !.FEJVN5FTU VTFGVMGPS !-BSHF5FTU
  22. . P D L 3 F U S P G

    J U T B N Q M F  D P E F  I U U Q T    H J U I V C  D P N  J [ V N J O      4 V O B [ V S J B Q Q  T S D  U F T U  L P U M J O  J O G P  J [ V N J O  B O E S P J E  T V O B [ V S J  J O G S B T U S V D U V S F  S F Q P T J U P S Z  T P V S D F  U F B N  5 F B N 3 F N P U F % B U B 4 P V S D F 5 F T U  L U .BTBZVLJ*;6.*!J[VNJO LZPCBTIJEFY