1

I'm using the Mosby Library to implement MVP pattern along with Retrofit and RxJava for my api calls almost exactly as it's done here.

I have just began with UI and Unit testing and while I've had no problems mocking my api during my UI testing (with MockWebServer), I can't figure out how to mock my api for Unit testing.

I've looked at the answer here but it seems like it's still trying to make the real api call since I see it's landing on onError (network exception) instead of onNext/onComplete. If I user MockWebServer it will also land on onError but not the network type.

What I want: When the presenter makes the api call :

signUpSubscriber = api.signUpUser(credentials)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Subscriber<User>()

To not actually make the call, but rather to be able to give it the "User" object it's supposed to return so that the test can pass on to onNext and onComplete

Community
  • 1
  • 1
kkl260
  • 375
  • 2
  • 17
  • 1
    so just replace the api with mock one that return an user Object, what kind of testing done here? are you testing the presenter and you don't care about the real network fetching logic? – yosriz Mar 28 '17 at 20:20
  • I'm using MockWebServer to return the user object but it keeps landing in onError for some reason. Correct about just testing the presenter (I want to make sure singUpSuccessful is called if the return object is a correct user (201 code), and onError is called if the return json is an error (400) – kkl260 Mar 28 '17 at 20:26
  • What is the error you get? Can you add the error's stack trace? Does your Retrofit API connects to the mock server or to the real server when calling `api.signUpUser(credentials)` in your UI test right now? – sockeqwe Mar 29 '17 at 10:35
  • As @yosriz said, just use Mockito to stub your desired api behaviour, and use this mock for method invocation verification. – Than Mar 29 '17 at 12:24
  • @Than no, don't do that, because that doesn't test service connection errors, server status codes (i.e. returning 500 or 404) etc. Use `MockWebServer` but you have to ensure that api is connecting to MockWebServer (change the base url to point to your mock server). – sockeqwe Mar 30 '17 at 11:12
  • @sockeqwe What for? From what I see he wants to unit test presenter not to make whole integration testing. Looking at the perspective of presenter there is either item and success or some error and exception. If he want's to simulate different errors he can just stub api with different `Observable.error(customError)`. No reason for this test to setup whole networking abstraction underneath. – Than Mar 30 '17 at 11:21
  • Moreover, by having mockwebserver directly your presenter tests will obtain knowledge about your data source, HTTP status codes, etc. Presenter (and it's tests) as a presentation logic shouldn't be aware about this informations. – Than Mar 30 '17 at 11:55
  • @Than here is a gist of what I am working with: https://gist.github.com/kkl260/cbd7ba20b20d7159b9abd39fabd3e9d3 I want to test the presenter.signUp method and basically test that if the object I send it is a 201 User object then it goes ahead to onNext/onComplete (which would reach MY onSignUpSuccess method). If I send a 400 error json then it would call onError. Here is what I'm looking for. Mockito.verify(view).showLoading(); Mockito.verify(view).signUpSuccessful(); It does get to showLoading successfully but for some reason I get a no internet Error and not to signupSuccessful – kkl260 Mar 30 '17 at 13:05
  • What's wrong with ``when(mApi.signUpUser()).thenReturn(Observable.just())``? – Fred Apr 04 '17 at 07:40
  • There was something I was doing in my presenter that was causing the issue. I wasn't properly injecting my api. Thanks everyone for the help. – kkl260 Apr 04 '17 at 12:16

0 Answers0