Skip to content

Instantly share code, notes, and snippets.

@tolmachevroman
Last active November 26, 2017 05:10
Show Gist options
  • Save tolmachevroman/9992d1dd5433219475c3008959d6215d to your computer and use it in GitHub Desktop.
Save tolmachevroman/9992d1dd5433219475c3008959d6215d to your computer and use it in GitHub Desktop.
Medium Part 3. WebServiceTest
@RunWith(JUnit4::class)
class WebServiceTest {
@Rule
@JvmField
var instantExecutor = InstantTaskExecutorRule()
private lateinit var webService: WebService
private lateinit var mockWebServer: MockWebServer
@Before
fun setup() {
mockWebServer = MockWebServer()
mockWebServer.start()
webService = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create())
.baseUrl(mockWebServer.url("/"))
.client(OkHttpClient())
.build()
.create(WebService::class.java)
}
@After
fun tearDown() {
mockWebServer.shutdown()
}
@Test
fun getRestaurantsTest() {
val content = this.javaClass.classLoader.getResource("get-restaurants-cases/valid.json").readText(Charset.forName("UTF-8"))
mockWebServer.enqueue(MockResponse()
.setResponseCode(200)
.setBody(content))
val restaurant = Restaurant(id = 1, cuisine = 1, name = "Mi Cevichazo", lat = -33.431756, lng = -70.578719, price = 8900,
image = "https://www.atrapalo.cl/common/photo/res/44559/106814/ticr_0_0.jpg", description = "Todo el sabor del Perú está presente en el restaurante Mi Cevichazo.")
val response = webService.getRestaurants().execute()
Assert.assertTrue(response.isSuccessful)
Assert.assertNotNull(response.body())
Assert.assertEquals(response.body()!!.first(), restaurant)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment