标签: robolectric

是否有更改目录的JVM命令行选项?

总之,我想知道,是否有一个JVM命令行选项来更改起始工作目录?如果它很重要,我正在运行jdk1.6.0_24.

背景:

我正在使用一个名为Robolectric for Eclipse的工具来测试主机PC上的Android应用程序而不进行仿真.Robolectric需要创建一个测试项目,但测试本身要从正在测试的Android项目中运行.这是通过Eclipse完成的,将运行配置设置为测试中的项目:"在所选项目,包或源文件夹中运行所有测试".

我想如果可能的话,从JVM命令行选项做到这一点,因为其他的工具,我用,Infinitest,不允许你指定测试的工作目录.但是,它允许您指定JVM命令行选项.

解决方法:

在找到更好的解决方法之前,一个成功的方法就是从Android项目中复制AndroidManifest.xml和res文件夹.

java android jvm robolectric infinitest

1
推荐指数
1
解决办法
1万
查看次数

无法执行dex:多个dex文件定义Lorg/apache/maven/artifact/Artifact

我有一个与Robolectric的Android项目.

我的应用程序相关类被放入src/main/java并测试相关的类src/test/java.

我有这些测试依赖项 pom.xml

        <dependency>
            <groupId>org.robolectric</groupId>
            <artifactId>robolectric</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

从控制台构建和部署时,mvn package android:deploy一切正常.从Eclipse(Juno)启动项目时出现问题.

[2013-03-24 12:27:26 - Dex Loader] Unable to execute dex: Multiple dex files define Lorg/apache/maven/artifact/Artifact;
[2013-03-24 12:27:26 - my-app] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/apache/maven/artifact/Artifact;
Run Code Online (Sandbox Code Playgroud)

似乎仅包括Robolectric所需的依赖性.我究竟做错了什么?

android robolectric android-annotations

1
推荐指数
1
解决办法
1万
查看次数

在Activity.onCreate()上使用Robolectric的NullPointerExcepcion

我正在使用Robolectric来测试扩展BaseActivity的Activity(FooActivity),而BaseActivity正在扩展Activity,但我在Activity.onCreate()中得到一个NullPointerException

FooActivity.java扩展了BaseActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
}
Run Code Online (Sandbox Code Playgroud)

BaseActivity.java扩展了Activity

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.mActivityCreated = true;
    mFragmentMgr = getFragmentManager();
    this.setContentView(R.layout.main);
}
Run Code Online (Sandbox Code Playgroud)

测试:FooActivityTest

@Before
public void setUp() {
    mActivity = new FooActivity();
}


@Test
public void test() {

    Intent i = new Intent();
    i.putExtra("isTesting", true);
    mActivity.setIntent(i);
    mActivity.onCreate(null);
}
Run Code Online (Sandbox Code Playgroud)

错误

java.lang.NullPointerException
at android.app.Activity.onCreate(Activity.java:874)
at com.pdi.enjoy.activities.BaseActivity.onCreate(BaseActivity.java:129)
at com.pdi.enjoy.activities.FooActivityTest.onCreate(FooActivity.java:107)
at com.pdi.enjoy.activities.FooActivityTest.test(FooActivity.java:32)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) …
Run Code Online (Sandbox Code Playgroud)

java android nullpointerexception junit4 robolectric

1
推荐指数
1
解决办法
1882
查看次数

使用 Robolectric 进行片段测试时出现“活动已被破坏”错误

我正在尝试Fragment在 Android 上进行测试Robolectric,我知道还有一些其他帖子关于FragmentActivity当它没有存储在某个变量中时的问题。

但是,我试图使这篇文章起作用,您可以看到在这种情况下FragmentActivity对象引用被保留,因此应该没有问题。这是基类:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import org.junit.After;
import org.robolectric.Robolectric;
import org.robolectric.util.ActivityController;

public class FragmentTestCase<T extends Fragment> {

private static final String FRAGMENT_TAG = "fragment";

private ActivityController controller;
private FragmentActivity activity;
private T fragment;

public void startFragment(T fragment) {
    this.fragment = fragment;
    controller = Robolectric.buildActivity(FragmentActivity.class);
    activity = (FragmentActivity) controller.create().start().visible().get();
    activity.getSupportFragmentManager()
            .beginTransaction()
            .add(fragment, FRAGMENT_TAG).commit();
}

@After
public void destroyFragment() {
    if (fragment != null) {
        activity.getSupportFragmentManager()
                .beginTransaction()
                .remove(fragment)
                .commit(); …
Run Code Online (Sandbox Code Playgroud)

android android-fragments robolectric

1
推荐指数
1
解决办法
1151
查看次数

使用AndroidAnnotations的Robolectric使测试挂起/不运行

我正在尝试使用Robolectric库配置基本应用程序.我使用的是lattest版本(3.0)以及最新的AndroidAnnotations版本(3.3.2)和Gradle 1.4.0.

在遇到AndroidHttpClient异常等少数问题后,我设法完成了一项非常简单的测试.

现在我想用Activity进行进一步测试,但我无法进行测试.测试以某种方式挂起并且永不屈服(每次测试都是黄色旋转).

这似乎是因为@Before设置,因为它在评论时产生.但为什么我不能使用这个注释设置我的活动?特别是当我使用带注释的Activity(而不是生成的Activity_)时,它会产生,这不是使用AndroidAnnotations时测试的正确方法.

这是我的截断测试类仍然无法执行...:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public class MapActivityTest {

  private MapActivity_ mMapActivity;

  @Before
  public void setup() {
    mMapActivity = Robolectric.buildActivity(MapActivity_.class).create().get();
  }

  @Test
  public void checkActivityNotNull() throws Exception {
    assertNotNull(mMapActivity);
  }

}
Run Code Online (Sandbox Code Playgroud)

如果可能有任何帮助,我也在使用Java 1.8.

有谁知道什么可能导致这个问题?

谢谢!

更新:

在降级到Java 7并尝试相同结果后尝试.

java android robolectric android-annotations

1
推荐指数
1
解决办法
767
查看次数

无法在 android studio 中为 robolectric 创建“test”文件夹

我想在 intellij 中为我的项目添加 robolectric 测试。

我试图按照本教程

但我不能在我的主模块下添加“测试”包。

此外,“构建变体”看起来与教程中不同。

我怎样才能解决这个问题?

如何正确添加测试类?

在此处输入图片说明

android unit-testing intellij-idea robolectric robolectric-gradle-plugin

1
推荐指数
1
解决办法
3162
查看次数

在Docker容器内运行时,零星的Robolectric测试失败

我正在努力将我们的Android团队转移到基于云的CI解决方案上.公司内部的其他部门已经完成了这项任务,我们有一个企业TeamCity许可证,利用安装了Docker的Linux EC2.整个公司使用TC,所以虽然CircleCI等是很好的选择,但我正在探索使用我们自己的TC解决方案.

到目前为止,它已经运行良好并使用Amazon ECR,我们托管了我们的Docker镜像,其中安装了所需的Android SDK,Gradle和Java组件.由于运行一些Robolectric测试用例时出错,我们的构建大部分时间都是正常的,但仍然比我们的物理本地代理更失败.谁看过这个吗?

java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: Landroid/content/res/Resources;
java.lang.NoClassDefFoundError: Landroid/content/res/Resources;
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
    at java.lang.Class.getDeclaredFields(Class.java:1916)
    at org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)
    at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)
    at org.junit.runners.model.TestClass.<init>(TestClass.java:57)
    at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:83)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at org.robolectric.internal.SandboxTestRunner$HelperTestRunner.<init>(SandboxTestRunner.java:242)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner.<init>(RobolectricTestRunner.java:467)
    at org.robolectric.RobolectricTestRunner.getHelperTestRunner(RobolectricTestRunner.java:319)
    at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:188)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36)
    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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:63)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    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:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) …
Run Code Online (Sandbox Code Playgroud)

java android amazon-ec2 robolectric docker

1
推荐指数
1
解决办法
1388
查看次数

Robolectric: “AndroidManifest.xml not found” and "Unable to find resource ID #0x7f09001b"

I'm running some tests with Roboletric, but I came across a issue that I can't solve. When I run the test, the following error appears with the "AndroidManifest":

WARNING: No manifest file found at .\AndroidManifest.xml.

Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE).

No such manifest file: .\AndroidManifest.xml

I've tried these solutions that failed:

@Config (manifest = Config.DEFAULT_MANIFEST_NAME)

@Config(manifest = Config.NONE, constants = BuildConfig.class, sdk = 26)

@Config( constants …
Run Code Online (Sandbox Code Playgroud)

android compiler-errors android-manifest robolectric

1
推荐指数
1
解决办法
994
查看次数

无法在包 [android, org.robolectric.default] 中找到资源 ID #0x7f09001c

我正在使用 Roboletric 运行一些测试,但遇到了一个我无法解决的问题。当我运行测试时,出现以下错误:

android.content.res.Resources$NotFoundException:无法在包 [android, org.robolectric.default] 中找到资源 ID #0x7f09001c

在 org.robolectric.shadows.ShadowAssetManager.getResName(ShadowAssetManager.java:925)

在 org.robolectric.shadows.ShadowAssetManager.loadXmlResourceParser(ShadowAssetManager.java:439)

在 org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:221)

在 android.content.res.Resources.loadXmlResourceParser(Resources.java)

在 android.content.res.Resources.getLayout(Resources.java:852)

在 android.view.LayoutInflater.inflate(LayoutInflater.java:394)

在 android.view.LayoutInflater.inflate(LayoutInflater.java:352)

在 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)

在 android.app.Activity.setContentView(Activity.java:1867)

在 com.example.robertoassad.roboletrictest.MainActivity.onCreate(MainActivity.java:15)

在 android.app.Activity.performCreate(Activity.java:5008)

在 org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:232)

在 org.robolectric.android.controller.ActivityController$1.run(ActivityController.java:58)

在 org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:364)

在 org.robolectric.shadows.CoreShadowsAdapter$1.runPaused(CoreShadowsAdapter.java:26)

在 org.robolectric.android.controller.ActivityController.create(ActivityController.java:55)

在 org.robolectric.android.controller.ActivityController.create(ActivityController.java:65)

在 org.robolectric.android.controller.ActivityController.setup(ActivityController.java:157)

在 org.robolectric.Robolectric.setupActivity(Robolectric.java:101)

在 com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)

在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

在 org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:523)

在 org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:226)

在 org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)

在 org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)

在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

在 org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:62)

在 org.junit.runners.ParentRunner.run(ParentRunner.java:363)

在 …

junit android unit-testing robolectric android-activity

1
推荐指数
1
解决办法
2201
查看次数

robolectric,没有意图匹配`receiveResult`

看我的代码:

doSomeActionWillStartNewIntentToSelectImage();

// check started intent
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
Assert.assertThat(activity, new StartedMatcher(intent));

// simulate the returning result
shadowOf(activity).receiveResult(
        new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI),
        Activity.RESULT_OK,
        new Intent().setData(activity.drawableId2Uri(R.drawable.icon_ok)));
Run Code Online (Sandbox Code Playgroud)

但它报告错误:

java.lang.RuntimeException: No intent matches 
    Intent{action=android.intent.action.PICK, data=content://media/internal/images/media} among 
   [Intent{action=android.intent.action.PICK, data=content://media/internal/images/media}]
at com.xtremelabs.robolectric.shadows.ShadowActivity.receiveResult(ShadowActivity.java:381)
Run Code Online (Sandbox Code Playgroud)

奇怪的Intent{action=android.intent.action.PICK, data=content://media/internal/images/media}是,它与后者完全相同.

错在哪里以及如何解决?

unit-testing android-intent robolectric

0
推荐指数
1
解决办法
842
查看次数