jeudi 8 novembre 2018

Androidx tests - How to set activity property before onCreate is called

i have a activity construct like this :

class MyActivity : AppCompatActivity() {
lateinit var model: MyModel

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_layout)

    if (!this::model.isInitialized) {
        model = MyModel.newInstance(this)
    }

    model.functionToCall()
}}

I want to test MyActivity with mock as model. Before Androidx-tests i use Robolectric (ActivityBuilder) to create a instance of MyActivity and set the model property with mock. In each test i start the activity lifecycle with ActivityBuiler.setup().

With Androidx-tests i try to use ActivityScenario or ActivityTestRule but it was impossible to set model property before this classes start the activity lifecycle

How can i do with Androidx-tests to set MyActivity.model property with mock before onCreate is called ? I can't use Androidx tests ?

Thanks

Aucun commentaire:

Enregistrer un commentaire