[英]Android Unit test fails with java.lang.IllegalArgumentException: INTERNET permission is required
I am trying to set up unit tests for my application.我正在尝试为我的应用程序设置单元测试。 But I am facing an issue with Segment
但是我遇到了Segment的问题
This is my test class:这是我的测试 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: MyApplication class 正在像这样初始化 Segment:
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我在 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:像这样将
instance()
传递给 Segment:
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()
无论如何我都试过了,但它失败了,因为它无法解析
shadowApp.getApplicationContext()
I am using shadowApplication by adding this我通过添加这个来使用 shadowApplication
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.如果使用 ShadowApplication 是正确的方法,请帮助我了解正在发生的事情。 My understanding for this code snippet is that it is a sample Application class or something that can be used for Testing.
我对这段代码片段的理解是它是一个示例应用程序 class 或可用于测试的东西。 Please cmiiw.
请 cmiiw。
Edit: My AndroidManifest.xml already has inte.net permission <uses-permission android:name="android.permission.INTE.NET"编辑:我的 AndroidManifest.xml 已经有 inte.net 权限 <uses-permission android:name="android.permission.INTE.NET"
Well apparently there was something wrong with my setup很明显我的设置有问题
I added these我加了这些
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.13.2'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.4.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:4.5.0'
// Optional -- mockito-kotlin
testImplementation "org.mockito.kotlin:mockito-kotlin:4.0.0"
// Optional -- Mockk framework
testImplementation 'io.mockk:mockk:1.12.3'
testImplementation 'androidx.test.ext:junit-ktx:1.1.3'
testImplementation 'org.robolectric:robolectric:4.6.1'
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation 'android.arch.core:core-testing:1.1.1'
and changed my test class to be like this:并将我的测试 class 更改为如下所示:
@RunWith(MockitoJUnitRunner::class)
class LoginViewModelTest : TestCase() {
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
private lateinit var viewModel: LoginViewModel
@Before
public override fun setUp() {
super.setUp()
val mMyApplication = mock(Application::class.java)
val sharedPreferences = mock(SharedPreferences::class.java)
val authService = mock(AuthService::class.java)
val analyticsFacade = mock(AnalyticsFacadeImpl::class.java)
doNothing().`when`(analyticsFacade).trackEvent(anyString(), any())
viewModel = LoginViewModel(
mMyApplication, sharedPreferences, authService,
analyticsFacade as AnalyticsFacadeImpl)
}
@Test
fun test_phone(){
viewModel.isPhone.observeForTesting {
val isPhone = LiveDataTestUtil.getValue(viewModel.isPhone) ?:
return@observeForTesting
Assert.assertFalse(isPhone)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.