2017-11-02 5 views
0

나는이 테스트 클래스가 있습니다왜이 Android 계측 테스트가 onCreate 활동을 두 번 호출합니까?

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 호출을 조롱하려고한다는 것입니다.하지만 어디에서 오는지 확실하지 않은 다른 로그가 있습니다. 참고로, 여기 당신이 볼 수 있듯이 로그 캣

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을 명중하고 그 후에 내 모의 서버를 누르십시오.

+0

당신이 ActivityTestRule의 코드를 추가 할 수 있습니다 설정해야합니다? 어쩌면 당신도 거기에 활동을 시작하고있다 – cherif

+0

나는 그것을 알아 냈다. 생성자에게 몇 가지 인수를 전달해야했습니다. 감사! – ndland

답변

0

나는 생성자에서 몇 가지 인수를 놓쳤다 ... Doh.

ActivityTestRule(InspirationalQuoteActivity::class.java) 

변경

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

트릭을했다. 당신은 intentTestRule IntentsTestRule<>(InspirationalQuoteActivity.class, false, true);

세 번째 매개 변수는 launchActivity 인으로 활동을 시작하는