devaga
devaga

Reputation: 376

Expecting an exception in Android Espresso test - fails

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.

Upvotes: 4

Views: 1972

Answers (2)

Rub&#233;n
Rub&#233;n

Reputation: 103

I had a very similar error.

I check the params in my fragment with the requireNotNull method. And I want to check if the IllegalArgumentException is thrown when this specific extra value is not present. I'm using the ActivityScenario to test this, because the parameters are provided by the activity intent.

The fragment is throwing the exception as expected, but the test is not catching the exception and it is finishing at the very moment the exception is thrown. I tried with different approaches but it's always failing:

  • @Test(expected = IllegalArgumentException::class)
  • @Test(expected = Exception::class)
  • assertFailsWith<IllegalArgumentException>
  • assertFailsWith<Exception>
  • Using try catch black

Anyone know how to test this scenario?

Upvotes: 0

Bin Fan
Bin Fan

Reputation: 51

Use normal try catch solved my problem, i know the exception is nested. But we can still check it by cause:

var error: Throwable? = null
try {
    onView(something).perform(click())
} catch (e: Throwable) {
    error = e;
}
assert(error!!.cause is MySpecificException)
assert(error!!.cause!!.message == "My specific error message")

I have tried another approach, but i ended up with flaky tests result.

Upvotes: 1

Related Questions