为什么这个Android Instrumentation Test会在onCreate上调用两次活动?

时间:2017-11-02 15:57:27

标签: android mocking kotlin mockwebserver

我有这个测试类:

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一次,之后我就点击了我的模拟服务器。

2 个答案:

答案 0 :(得分:1)

我错过了构造函数中的一些参数...... Doh。

更改

ActivityTestRule(InspirationalQuoteActivity::class.java)

ActivityTestRule(InspirationalQuoteActivity::class.java, false, false)

做了这个伎俩。

答案 1 :(得分:0)

您正在使用intentTestRule启动活动 IntentsTestRule<>(InspirationalQuoteActivity.class, false, true);

第三个参数是launchActivity,您必须将其设置为false