Home > other >  Lateinit variable is not initialized in TestNG's @BeforeSuite
Lateinit variable is not initialized in TestNG's @BeforeSuite

Time:10-27

I am trying to lateinit variable (AppiumDriver service) for my testNG tests in @BeforeSuite method like this:

abstract class BaseTest {

    lateinit var driver: AppiumDriver<MobileElement>

    @Parameters("platform")
    @BeforeSuite(alwaysRun = true)
    fun init(platform: String) {
        Configuration.isIOS = platform.lowercase() == "ios"
        driver = <driver initialization>
        println(driver)
    }
}

My tests then look like this:

class MedicationsTests : BaseTest() {
    fun test_01() {
        // Test using `driver` variable
    }
}

@BeforeSuite method init(String) is running before tests start (I see output of print), but then I am getting errror, when method test_01() is trying to use driver variable:

kotlin.UninitializedPropertyAccessException: lateinit property driver has not been initialized

It is working perfectly fine in @BeforeClass, but I want to run this initialization only once per whole suite, not once per class...

Any ideas how to resolve it?

CodePudding user response:

TestNG will create a new instance of BaseTest class for you per each test. If you want to share your driver - make it static. Example:

abstract class BaseTest {
    companion object {
        lateinit var driver: AppiumDriver<MobileElement>
    }
}
  • Related