I have this test class:
class InspirationalQuoteInstrumentedTest {
private lateinit var server: MockWebServer
@Rule
@JvmField
val mActivityRule: ActivityTestRule<InspirationalQuoteActivity> = ActivityTestRule(InspirationalQuoteActivity::class.java)
@Before
fun setUp() {
server = MockWebServer()
server.start()
Constants.BASE_URL = server.url("/").toString()
}
@After
fun tearDown() {
server.shutdown()
}
@Test
fun ensureTheQuoteOfTheDayIsDisplayed() {
println("Base URL: ${Constants.BASE_URL}")
Log.e(TAG,"Base URL: ${Constants.BASE_URL}")
val response200 = this::class.java.classLoader.getResource("200.json").readText()
val jsonResponse = JSONObject(response200)
val expectedQuote = jsonResponse
.getJSONObject("contents")
.getJSONArray("quotes")
.getJSONObject(0)
.getString("quote")
server.enqueue(MockResponse()
.setResponseCode(200)
.setBody(response200))
val intent = Intent()
mActivityRule.launchActivity(intent)
onView(withId(R.id.inspirationalQuote))
.check(matches(withText(expectedQuote)))
}
companion object {
val TAG = InspirationalQuoteInstrumentedTest::class.java.simpleName
}
}
And I have this activity:
class InspirationalQuoteActivity : AppCompatActivity() {
private lateinit var quoteService: QuoteOfTheDayService
private var quote: String = ""
private var author: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_inspirational_quote)
val textView = findViewById<TextView>(R.id.inspirationalQuote) as TextView
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
textView.text = getQuoteOfTheDay()
}
private fun getQuoteOfTheDay(): String {
quoteService = QuoteOfTheDayService()
val qod = quoteService.getQuoteOfTheDay()
val response = qod.execute()
Log.e(TAG, "Response: $response")
response?.let {
quote = response.body()!!.contents.quotes[0].quote
author = response.body()!!.contents.quotes[0].author
}
Log.e(TAG, "Expected Quote: $quote")
return quote
}
companion object {
private val TAG = InspirationalQuoteActivity::class.java.simpleName
}
}
When I run my test getQuoteOfTheDay()
gets executed twice. What gives? The issue is that I'm trying to mock out an api call which looks like it's working ask expected, however there is another log that I'm not sure where it's coming from. For reference, here is the out put in logcat
Response: Response{protocol=http/1.1, code=200, message=OK, url=https://quotes.rest/qod}
Expected Quote: Let us think the unthinkable, let us do the undoable, let us prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Response: Response{protocol=http/1.1, code=200, message=OK, url=http://localhost:37290/qod}
Expected Quote: Winning is nice if you don't lose your integrity in the process.
As you can see, I hit https://quotes.rest/qod
once, and then I hit my mock server after that.
I missed some arguments in the constructor... Doh.
Changing
ActivityTestRule(InspirationalQuoteActivity::class.java)
to
ActivityTestRule(InspirationalQuoteActivity::class.java, false, false)
did the trick.