简体   繁体   中英

Robolectric.setupActivity() is deprecated in Android unit test

I'm trying to use Robolectric framework to run a simple Unit test, although I got that Robolectric.setupActivity() is deprecated.

@RunWith(RobolectricTestRunner.class)
public class MainActivityFragmentTest {

    MainActivity mainActivity;

    @Before
    public void setUp() {
        mainActivity = Robolectric.setupActivity(MainActivity.class);
    }

    // Rest of Test

How can I solve that? Thanks for help in advance..

Use ActivityScenario, documetation is here: ActivityScenario

try(ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class)) {
 scenario.onActivity(activity -> {
   assertThat(activity.getSomething()).isEqualTo("something");
 });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM