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