title: How to make MockWebServer work?
body: <p>I am developing an app using the MVP architecture. I am trying to test the Interactors of my app using MockWebServer. Well, I have this test:</p>
  
  <pre><code>@RunWith(RobolectricTestRunner::class)
  @Config(constants = BuildConfig::class, manifest = "src/main/AndroidManifest.xml", packageName = "br.com.simplepass.simplepassnew", sdk = intArrayOf(23))
  class LoginInteractorImplTest {
  
      lateinit var mLoginInteractor : LoginInteractor
      lateinit var mServer: MockWebServer
  
  
      @Before
      fun setUp(){
          mLoginInteractor = LoginInteractorImpl()
          mServer = MockWebServer()
          mServer.start()
      }
  
      @Test
      fun loginTest(){
          mServer.url("http://192.168.0.10:8080/login")
  
          val testSubscriber = TestSubscriber.create&lt;User&gt;()
  
          mLoginInteractor.login("31991889992", "lala").subscribe(testSubscriber)
          testSubscriber.assertNoErrors()
  //        testSubscriber.assertCompleted()
      }
  
      @After
      fun tearDown(){
          mServer.shutdown()
      }
  }
  </code></pre>
  
  <p>But, when I uncomment the assertCompleted on the TestSubscriber, I always get assertionError... I know the TestSubscriber works, because I use it in other tests. </p>
  
  <p>Here is my ApiCall:</p>
  
  <pre><code>@GET("login")
  fun login() : Observable&lt;User&gt;
  </code></pre>
  
  <p>My NetModule:</p>
  
  <pre><code>@Module
  class NetModule(val mBaseUrl: String) {
  
      @Provides
      @Singleton
      fun provideHttpCache(application: Application): Cache {
          val cacheSize = 10 * 1024 * 1024
          return Cache(application.cacheDir, cacheSize.toLong())
  
      }
  
      @Provides
      @Singleton
      fun provideOkhttpClient(cache: Cache) : OkHttpClient {
          val client = OkHttpClient.Builder()
  
          val interceptor = HttpLoggingInterceptor()
          interceptor.level = HttpLoggingInterceptor.Level.BODY
  
          client.addInterceptor(interceptor)
          return client.cache(cache).build()
      }
  
      @Provides
      @Singleton
      fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
          return Retrofit.Builder()
  
  
            .baseUrl(mBaseUrl)
              .addConverterFactory(GsonConverterFactory.create())
              .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
              .client(okHttpClient)
              .build()
      }
  }
  </code></pre>
  
  <p>And my base URL (There's no backend server... could be anything):</p>
  
  <pre><code>&lt;string name="api_base_url"&gt;http://192.168.0.12:8080&lt;/string&gt;
  </code></pre>
  
  <p>So, What am I missing? This code should be working...</p>
  
  <p>Any help is welcome!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>So, I changed the code to this:</p>
  
  <pre><code>mLoginInteractor = LoginInteractorImpl()
          mServer = MockWebServer()
  
          mServer.enqueue(MockResponse()
                  .setResponseCode(200)
                  .setBody(Gson().toJson(User(1, "991889992", "Leandro", "123"))))
  
          mServer.start()
  
          val client = OkHttpClient.Builder()
          val cacheSize = 10 * 1024 * 1024
  
          client.cache(Cache(application.cacheDir, cacheSize.toLong())).build()
  
          mLoginInteractor.setRetrofit(Retrofit.Builder()
                  .baseUrl(mServer.url("/"))
                  .addConverterFactory(GsonConverterFactory.create())
                  .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
  .client(client.cache(Cache(application.cacheDir, cacheSize.toLong())).build())
                  .build())
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>val testSubscriber = TestSubscriber.create&lt;User&gt;()
  
          mLoginInteractor.login("31991889992", "lala").subscribe(testSubscriber)
          testSubscriber.assertNoErrors()
          testSubscriber.assertReceivedOnNext(listOf(User(1, "991889992", "Leandro", "123")))
          testSubscriber.assertCompleted()
  </code></pre>
  
  <p>But I still get this error:</p>
  
  <pre><code>Number of items does not match. Provided: 1  Actual: 0.
  Provided values: [User(id=1, phoneNumber=991889992, name=Leandro, password=123)]
  Actual values: []
  </code></pre>