![](/img/trans.png)
[英]How can I hook onto onCreate of the Activity in an Android instrumentation test?
[英]Why is this Android Instrumentation Test invoking activity onCreate twice?
我有這個測試班:
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
}
}
我有這個活動:
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
}
}
當我運行測試時, getQuoteOfTheDay()
被執行兩次。 是什么賦予了? 問題是,我正在嘗試模擬一個api調用,該調用看起來像在正常工作,請按預期,但是還有另一個日志我不確定它來自哪里。 供參考,這是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.
如您所見,我點擊了https://quotes.rest/qod
一次,然后再點擊了我的模擬服務器。
我錯過了構造函數中的一些參數... Doh。
更改
ActivityTestRule(InspirationalQuoteActivity::class.java)
至
ActivityTestRule(InspirationalQuoteActivity::class.java, false, false)
做到了。
您正在使用intentTestRule IntentsTestRule<>(InspirationalQuoteActivity.class, false, true);
啟動活動IntentsTestRule<>(InspirationalQuoteActivity.class, false, true);
第三個參數是launchActivity
,必須將其設置為false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.