WitWicky
WitWicky

Reputation: 291

Robolectric not calling Application.onCreate()

I am very much new to Robolectric. I am facing these issues after migrating from Robolectric v3.0 to v3.4.1.

  1. Robolectric is not loading my AppController class which extends Application
  2. I am getting class cast exception on casting to RuntimeEnvironment.application to my AppController, which shouldn't be the case as my AppController extends Application and was working fine before migrating to latest version.

Please find the code below for my test

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "app/src/main/AndroidManifest.xml", sdk = 21)
public class FragmentTest {

private ExampleFragment fragment = SupportFragmentController.of(new ExampleFragment()).create().get();
private Context mContext;

@Before
public void setUp() throws Exception {
   // RuntimeEnvironment.application.onCreate();
   mContext =RuntimeEnvironment.application ;
}

@Test
public void testFragmentInstantiation() {
   ExampleFragment.mAppController = (AppController)mContext;

}

To reproduce - run any test.

Robolectric version is 3.4.1.

Upvotes: 1

Views: 523

Answers (1)

Eugen Martynov
Eugen Martynov

Reputation: 20130

The main advice is to do not put manifest file location in the @Config section unless you know what you're doing. So please change your test annotations to:

@Config(constants = BuildConfig.class, sdk = 21)

Also, I don't know if it is by purpose but you could use much newer android sdk in your test with the newest version of Robolectric

Upvotes: 1

Related Questions