androidjunitkotlinandroid-testing

JUnit tests always pass when executing in device


This simple test

@RunWith(JUnit4::class)
class Test {

    @Test
    fun test() {
        assert(false)
    }

}

Unexpectedly, this passes when put in androidTest (both through Android Studio and in the terminal), but obviously fails as expected when put in test.


Solution

  • You need to use JUnit assertions for running tests. The base assert() functionality is normally disabled when running "production" code, so you cannot depend that a plain assert statement will throw an assertion exception.

    Use:

    org.junit.Asserts.assertTrue( false )
    

    to make the test fail properly.