I am writing tests in Espresso for my Android application using Kotlin. I want to expect an exception as in the unit test and pass the test if such an exception is found.
I had a test that was checking for the Intent extras format. In the implementation I have thrown an exception after getting an incorrect value in the intent. I want to pass the test is this exception is thrown. Running the test results with a "Test failed" and java.lang.IllegalArgumentException which is the same as I am trying to catch.
I tried with this annotation at first:
@Test(expected = IllegalArgumentException::class)
Then I tried with a org.jetbrains.kotlin:kotlin-test dependency and the following assertion:
assertFailsWith<java.lang.IllegalArgumentException> {
val activityIntent = Intent()
activityIntent.putExtra("extra_connection_number", intentExtraConnectionNumber)
activityRule.launchActivity(activityIntent)
}
What may be also important is that the exception is thrown in the onCreateViewstarting as so:
@Throws(java.lang.IllegalArgumentException::class)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val layout = inflater.inflate(R.layout.configuration_fragment, container, false)
var connectionNumber: Int? =
activity!!.intent.getIntExtra(resources.getString(R.string.extra_connection_number), -1)
if (connectionNumber == -1) {
throw IllegalArgumentException()
}
(...)
The intent is created with the given extra, and it is failing in a place I wanted it to fail. But the thrown exception is not caught by the Espresso. Why? What am I missing?
I am aware it may be incompatible with Espresso, but I cannot find a right documentation helping me with the problem.
Aucun commentaire:
Enregistrer un commentaire