1

Hello I am implementing and SDK, and I need a context for some pieces of code, so, for get access to a context, I implemented this in a Application class:

public class SDKApp extends Application {

    private static SDKApp sdkContext;

    public static Context getContext() {
        return sdkContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        sdkContext = this;

    }

}

For test this, I am using Robolectric, but the context is returning null value:

@RunWith(RobolectricTestRunner.class)
public class CallTest {

    private Context context = RuntimeEnvironment.application;

    @Test
    public void emailAuthenticationSuccessfulTest() {
        if (BuildConfig.FLAVOR.equals("dev")) {

            Context context = SDKApp.getContext();

            assertNotNull(context);
        }
    }
}

some idea about this?

Thanks

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84

2 Answers2

1

add you SDKApp Class into your manifest application tag like this

<application
        android:name="SDKApp" // your complete package path to your SDKApp
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
Ramzy Hassan
  • 926
  • 8
  • 21
1

Make sure your SDKApp is registered in AndroidManifest.xml like

<application
    android:name="SDKApp"

Then you may call SDKApp.getContext() otherwise you all time get null value from SDKApp.getContext()

Thanks.

Mimo Saha
  • 61
  • 4