gpt4 book ai didi

java - Robolectric 给我一个 java.lang.IllegalArgumentException : INTERNET permission is required

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:00 25 4
gpt4 key购买 nike

我正在为现有应用改造单元测试。当我运行这个简单的单元测试时

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import static junit.framework.Assert.assertTrue;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, packageName = "com.blah.blah" )
public class TestThis {

@Test
public void blah(){
assertTrue(true);
}
}

我收到这个错误

java.lang.IllegalArgumentException: INTERNET permission is required.
at com.segment.analytics.Analytics$Builder.<init>(Analytics.java:585)
at com.segment.analytics.Analytics.with(Analytics.java:115)
at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

java.lang.RuntimeException: java.lang.IllegalArgumentException: INTERNET permission is required.
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:244)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.IllegalArgumentException: INTERNET permission is required.
at com.segment.analytics.Analytics$Builder.<init>(Analytics.java:585)
at com.segment.analytics.Analytics.with(Analytics.java:115)
at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)

我的第一个假设是 robolectric 没有得到 list ,但它似乎在那里。有什么基本的东西是我遗漏的吗?

对不起,我写这篇文章的时候很匆忙,应该再补充一些细节

我在 list 中有这个

<uses-permission android:name="android.permission.INTERNET" />

我确实在阅读此页面后尝试明确设置 list Robolectric junit test - missing internet permission

另一个更新,问题是在 onCreate() 中触发的 http 调用

public class TestApp extends Application {

@Override
public final void onCreate() {
super.onCreate();
singleton = this;
if (!BuildConfig.DEBUG) { Fabric.with(this, new Crashlytics()); }
loadData();
// The next line makes the http call
Analytics.with(getApplicationContext()).identify("userId", null, null);
RequestQueues.initInstance(singleton);
}
}

有什么想法吗?我可以添加

if (notTest)
{
com.segment.analytics.Analytics.with(getApplicationContext()).identify("userId",
}

但宁愿不

最佳答案

显然,在使用 robolectric 运行单元测试时,不会启用权限。检查段库,它检查互联网权限如下:

 /** Returns true if the application has the given permission. */
public static boolean hasPermission(Context context, String permission) {
return context.checkCallingOrSelfPermission(permission) == PERMISSION_GRANTED;
}

但如前所述,运行测试的 em 您的应用程序将没有互联网权限(但是您将能够进行互联网通话)您可以通过向影子应用程序授予互联网权限然后在 Segment 中使用此上下文来解决这个问题图书馆。

将此方法添加到您的 TestApp

protected Context instance() {
ShadowApplication shadowApp = Shadows.shadowOf(this);
shadowApp.grantPermissions("android.permission.INTERNET");

return shadowApp.getApplicationContext();
}

然后把你的代码改成这样

Analytics.with(instance()).identify("userId", null, null);

希望对你有帮助。

关于java - Robolectric 给我一个 java.lang.IllegalArgumentException : INTERNET permission is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34190822/

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