-
-
Save kkl260/cbd7ba20b20d7159b9abd39fabd3e9d3 to your computer and use it in GitHub Desktop.
Unit Test Retrofit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface BaseApi | |
@POST("register") | |
Observable<User> signUpUser(@Body User auth); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseApiManager { | |
private BaseApi mAppApi; | |
private String BASE_URL; | |
public BaseApi getAppApi(){ | |
if (BuildConfig.IS_RELEASE){ | |
BASE_URL = Constants.PRODUCTION_URL; | |
} | |
else { | |
BASE_URL = Constants.STAGING_URL; | |
} | |
if (mAppApi == null){ | |
OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); | |
builder.readTimeout(15, TimeUnit.SECONDS); | |
builder.connectTimeout(10, TimeUnit.SECONDS); | |
builder.writeTimeout(10, TimeUnit.SECONDS); | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); | |
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
builder.addInterceptor(interceptor); | |
// Create GsonFireBuilder | |
GsonFireBuilder fireBuilder = new GsonFireBuilder(); | |
fireBuilder.registerPreProcessor(Expandable.class, new ExpandablePreprocessor()); | |
fireBuilder.registerPostProcessor(RealmObject.class, new RealmPostProcessor()); | |
// Create GsonBuilder | |
GsonBuilder gsonBuilder = fireBuilder.createGsonBuilder(); | |
// Configure gsonBuilder as usual | |
// Create gson for GsonBuilder | |
Gson gson = gsonBuilder.create(); | |
mAppApi = new Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create()) | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.client(builder.build()) | |
.build() | |
.create(BaseApi.class); | |
} | |
return mAppApi; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SignUpPresenter extends MvpBasePresenter<SignUpView> { | |
private Subscription signUpSubscriber; | |
private EventBus eventBus; | |
private SignUpView view; | |
private BaseApi api; | |
@Inject | |
public SignUpPresenter(EventBus eventBus, BaseApi api) { | |
this.eventBus = eventBus; | |
this.api = api; | |
} | |
public void doSignUp(final User credentials, final Context context) { | |
if (isViewAttached()) { | |
getView().showLoading(); | |
} | |
cancelSubscription(); | |
api = new BaseApiManager().getAppApi(); | |
signUpSubscriber = api.signUpUser(credentials) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribeOn(Schedulers.io()) | |
.subscribe(new Subscriber<User>() { | |
@Override | |
public void onCompleted() { | |
getView().signUpSuccessful(); | |
} | |
@Override | |
public void onError(Throwable e) { | |
if (isViewAttached()) { | |
if (e instanceof RetrofitException) { | |
RetrofitException error = (RetrofitException) e; | |
if (error.getKind() == RetrofitException.Kind.NETWORK) { | |
//handle network error | |
Timber.d("NETWORK ERROR"); | |
getView().showError(context.getString(R.string.no_internet)); | |
} else { | |
//handle error message from server | |
Timber.d(e.getLocalizedMessage()); | |
Error response = null; | |
try { | |
response = error.getErrorBodyAs(Error.class); | |
String errorString = response.getErrorString(); | |
Timber.d("Error = " + errorString); | |
// FINISH API CALL | |
getView().showError(errorString); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
} | |
} | |
} | |
@Override | |
public void onNext(final User user) { | |
// we make another api call to login here.. | |
} | |
}); | |
} | |
/** | |
* Cancels any previous callback | |
*/ | |
private void cancelSubscription() { | |
if (signUpSubscriber != null && !signUpSubscriber.isUnsubscribed()) { | |
signUpSubscriber.unsubscribe(); | |
} | |
} | |
@Override public void detachView(boolean retainInstance) { | |
super.detachView(retainInstance); | |
if (!retainInstance) { | |
cancelSubscription(); | |
} | |
} | |
@Override public void attachView(SignUpView view) { | |
super.attachView(view); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(MockitoJUnitRunner.class) | |
public class SignUpPresenterTest { | |
private SignUpPresenter presenter; | |
@Mock | |
private SignUpView view; | |
@Mock | |
Context mMockContext; | |
@Mock | |
private EventBus bus; | |
@Mock | |
BaseApi mApi; | |
User user; | |
@Rule | |
public final RxSchedulersOverrideRule mOverrideSchedulersRule = new RxSchedulersOverrideRule(); | |
@Before | |
public void setUp() throws Exception { | |
presenter = new SignUpPresenter(bus, mApi); | |
presenter.attachView(view); | |
} | |
@Test | |
public void signUp() throws Exception { | |
//Act | |
user = new User(); | |
user.register("username@email.com", "1234"); | |
User output = new User(); | |
output.setFirst_name("new"); | |
output.setLast_name("user"); | |
output.setId(0); | |
BaseApi api = Mockito.mock(BaseApi.class); | |
Retrofit retrofit = Mockito.mock(Retrofit.class); | |
Mockito.when(retrofit.create(BaseApi.class)).thenReturn(api); | |
//MyService service = new MyService(retrofit); | |
when(api.signUpUser(user)).thenReturn(Observable.just(output)); | |
presenter.doSignUp(user, mMockContext); | |
//Assert | |
Mockito.verify(view).showLoading(); | |
Mockito.verify(view).signUpSuccessful(); | |
} | |
@After | |
public void tearDown() throws Exception { | |
} | |
@After | |
public void detachView() { | |
presenter.detachView(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment