zak.taccardi
09/16/2019, 3:03 PMprivate lateinit var dog: Dog
@Before fun setUp() {
dog = Dog()
}
there would be:
private val dog by before { Dog() } // constructs `Dog` instance once per test
wasyl
09/16/2019, 3:05 PMstreetsofboston
09/16/2019, 3:15 PMclass MyDogTest {
private val dog = Dog()
...
@Test fun someTest() { .... dog .... }
...
}
?
Most (if not all) unit-test-runners I know always create a brand new instance of MyDogTest
for every unit-test function that is executed.Paul Woitaschek
09/16/2019, 3:23 PMwasyl
09/16/2019, 3:23 PMVlad
09/16/2019, 3:42 PMandroid.Viewmodel
and there is something in the init {} block that runs the coroutine or observes the flow.wasyl
09/16/2019, 3:44 PMinit
block, it’s initialized firstVlad
09/16/2019, 3:46 PMcoroutines-test
it's the `Dispatchers::setMain`invocation that is required to set the main dispatcher... But I guess it could also be injectedwasyl
09/16/2019, 3:47 PMinit
block before the property declaration (and another init block later if you want)streetsofboston
09/16/2019, 3:48 PMsetMain
, but no such hooks such as setDefault
or setIO
… 🙂Mike
09/16/2019, 5:25 PMval dog = Dog()
is acceptable.
BUT some frameworks, including JUnit Jupiter allow for a change to that lifecycle so the test is only instantiated ONCE, and all test methods are then run.
https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle