. P D L 3 F U S P G J U
.BTBZVLJ*;6.*!J[VNJO
LZPCBTIJEFY
Slide 2
Slide 2 text
> Masayuki IZUMI a.k.a. @izumin5210
> Rekimoto Lab. at the Univ. of Tokyo
> Strobo, Inc. / Wantedly, Inc.
> Rubyist / Androider / {Java,Type}Scripter
Slide 3
Slide 3 text
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;
}
Slide 4
Slide 4 text
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 get(
@Query("access_token") String accessToken
);
}
Slide 5
Slide 5 text
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);
Slide 6
Slide 6 text
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...
})
Slide 7
Slide 7 text
) P X U P U F T U J U
) P X U P N P D L J U
Slide 8
Slide 8 text
% 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 get(String accessToken) {
User mockUser = new User();
// ...
return mockUser;
}
}
// write your test cases below...
Slide 9
Slide 9 text
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...
Slide 10
Slide 10 text
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();
Slide 11
Slide 11 text
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))
Slide 12
Slide 12 text
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))
Slide 13
Slide 13 text
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))
Slide 14
Slide 14 text
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))
Slide 15
Slide 15 text
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 delegate;
// snip the constructor
@Override Single get(String accessToken) {
User mockUser = new User();
// ...
return delegate.returning(Calls.response(mockUser)).fetch(accessToken);
}
}
Slide 16
Slide 16 text
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 delegate;
// snip the constructor
@Override Single get(String accessToken) {
User mockUser = new User();
// ...
return delegate.returning(Calls.response(mockUser)).fetch(accessToken);
}
}
Slide 17
Slide 17 text
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 delegate;
// snip the constructor
@Override Single get(String accessToken) {
User mockUser = new User();
// ...
return delegate.returning(Calls.response(mockUser)).fetch(accessToken);
}
}
Call response(T)
Call response(Response)
Call failure(IOException)
Slide 18
Slide 18 text
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))
Slide 19
Slide 19 text
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);
Slide 20
Slide 20 text
$ P O D M V T J P O
5PNPDLNFUIPETPGUIFSFUPSpUJOUFSGBDF
JNQMFNFOUTNPDLDMBTTZPVSTFMG
TUVCTNFUIPET FH.PDLJUP
VTFT.PDL8FC4FSWFS
VTFT.PDL3FUSPpU
Slide 21
Slide 21 text
$ P O D M V T J P O
.PDL8FC4FSWFS
.PDL3FUSPpU
TUVCCJOHNFUIPET
NPDLDMBTTFT
TJNQMF qFYJCMF
˕
˕
˓ ˓
˚
˚ !4NBMM5FTU
!.FEJVN5FTU
VTFGVMGPS
!-BSHF5FTU
Slide 22
Slide 22 text
. 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