gpt4 book ai didi

Android 单元测试失败并出现 java.lang.IllegalArgumentException : INTERNET permission is required

转载 作者:行者123 更新时间:2023-12-04 23:42:53 28 4
gpt4 key购买 nike

我正在尝试为我的应用程序设置单元测试。但我遇到了 Segment 的问题
这是我的测试课:

@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)
}
}
}
这是失败并出现此错误:
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 类正在初始化 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)
}
我找到了 this在 StackOverflow 中
他们建议我们必须像这样初始化应用程序:
protected Context instance() {
ShadowApplication shadowApp = Shadows.shadowOf(this);
shadowApp.grantPermissions("android.permission.INTERNET");

return shadowApp.getApplicationContext();
}
通过 instance()像这样分割:
Analytics.with(instance()).identify("userId", null, null);
我认为这不是正确的方法,因为我们正在更改单元测试的代码,恕我直言,这不是正确的方法。
无论如何我都试过了,但它失败了,因为它无法解析 shadowApp.getApplicationContext()我通过添加这个来使用 shadowApplication
implementation 'org.robolectric:shadow-framework:4.6'
请帮我解决这个问题。提前致谢。
如果使用 ShadowApplication 是正确的方法,请帮助我了解正在发生的事情。我对这个代码 fragment 的理解是它是一个示例应用程序类或可用于测试的东西。请cmiiw。
编辑:
我的 AndroidManifest.xml 已经有互联网权限

最佳答案

显然我的设置有问题
我添加了这些

// 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'
并将我的测试类更改为:
@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)
}
}
}

关于Android 单元测试失败并出现 java.lang.IllegalArgumentException : INTERNET permission is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71845807/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com