Share
Embed
MockRetrofit
http://rmp-quipper.connpass.com/event/31744/
MockWebServer and MockRetrofit
izumin5210
May 27, 2016
Transcript
22 slides
-
. P D L 3 F U S P G
J U .BTBZVLJ*;6.*!J[VNJO LZPCBTIJEFY -
> Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at the
Univ. of Tokyo > Strobo, Inc. / Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter -
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; } -
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 ); } -
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); -
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... }) -
) P X U P U F T
U J U ) P X U P N P D L J U -
% 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... -
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... -
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(); -
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)) -
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)) -
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)) -
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)) -
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); } } -
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); } } -
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) -
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)) -
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); -
$ P O D M V T J P O
5PNPDLNFUIPETPGUIFSFUPSpUJOUFSGBDF JNQMFNFOUTNPDLDMBTTZPVSTFMG TUVCTNFUIPET FH.PDLJUP VTFT.PDL8FC4FSWFS VTFT.PDL3FUSPpU -
$ P O D M V T J P O
.PDL8FC4FSWFS .PDL3FUSPpU TUVCCJOHNFUIPET NPDLDMBTTFT TJNQMF qFYJCMF ˕ ˕ ˓ ˓ ˚ ˚ !4NBMM5FTU !.FEJVN5FTU VTFGVMGPS !-BSHF5FTU -
. 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