I am trying to set up unit tests for my application. But I am facing an issue with Segment
This is my test class:
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.O_MR1])
class LoginViewModelTest {
private lateinit var viewModel: LoginViewModel
@Mock
private lateinit var authService: AuthService
@Mock
private lateinit var sharedPreferences: SharedPreferences
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
val mMyApplication = mock<Application>()
viewModel = LoginViewModel(mMyApplication, sharedPreferences, authService)
}
@Test
fun test_phone(){
viewModel.isPhone.observeForTesting {
val isPhone = LiveDataTestUtil.getValue(viewModel.isPhone) ?:
return@observeForTesting
Assert.assertFalse(isPhone)
}
}
}
This is failing with this error:
INTERNET permission is required.
java.lang.IllegalArgumentException: INTERNET permission is required.
at com.segment.analytics.Analytics$Builder.<init>(Analytics.java:1068)
at com.app.android.MyApplication.onCreate(MyApplication.kt:42)
MyApplication class is initializing Segment like this:
override fun onCreate() {
val analytics: Analytics = Analytics.Builder( //This is where its failing
applicationContext,
this.getString(R.string.segmentApiKey)
)
.trackApplicationLifecycleEvents()
.build()
// Set the initialized instance as a globally accessible instance.
Analytics.setSingletonInstance(analytics)
}
I found this in StackOverflow
where they are suggesting that we have to init application like this:
protected Context instance() {
ShadowApplication shadowApp = Shadows.shadowOf(this);
shadowApp.grantPermissions("android.permission.INTERNET");
return shadowApp.getApplicationContext();
}
pass instance()
to Segment like this:
Analytics.with(instance()).identify("userId", null, null);
I don't think this is the right way because we are altering the code for the Unit test which is not the correct way IMHO.
I tried it anyway, but it's failing because it cannot resolve shadowApp.getApplicationContext()
I am using shadowApplication by adding this
implementation 'org.robolectric:shadow-framework:4.6'
Please help me figure this out. Thanks in advance.
If using ShadowApplication is the right way, please help me understand what is happening too. My understanding for this code snippet is that it is a sample Application class or something that can be used for Testing. Please cmiiw.
Edit: My AndroidManifest.xml already has internet permission <uses-permission android:name="android.permission.INTERNET"
Well apparently there was something wrong with my setup
I added these
and changed my test class to be like this: