m0skit0
m0skit0

Reputation: 25874

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.

Upvotes: 1

Views: 1605

Answers (2)

gluttony
gluttony

Reputation: 569

I just fall on same issue, on a newer version of junit I then used quite the same code as accepted answer with org.junit.jupiter.api.Assertions.assertTrue(false) but IntelliJ told me there is a simpler method with:

org.junit.jupiter.api.Assertions.fail();

Upvotes: 1

Bob Dalgleish
Bob Dalgleish

Reputation: 8227

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.

Upvotes: 6

Related Questions