Problem using BeforeClass with Robolectric

13 Views Asked by At

I am learning Robolectric to check some unit tests in my Android project with Kotlin and faced the next problem.

When I use the annotation @Before and @After the functions that setup my variables are executed, but when I try it with the annotations @BeforeClass and @AfterClass they are never executed.

I tried to search information about the problem and find out that those annotations must be static so I implemented them in this way:

companion object {
    private lateinit var tests: ArrayList<com.ifun.furor.model.tests.Test>

    @BeforeClass @JvmStatic
    fun setUpCommon() {
        println("beforeClass")
        tests = TestsProvider(ApplicationProvider.getApplicationContext()).getTests()
    }

    @AfterClass @JvmStatic
    fun tearDownCommon() {
        println("afterClass")
        tests = arrayListOf()
    }

}

But when I run the test class with (for example) the next test:

@Test
fun checkTestsSize() {
    println("checkTestSize")
    assertThat(tests.size, `is`(210))
}

I find out that checkTestsSize is executed right before the setUpCommon function. Why can it be?

0

There are 0 best solutions below