我正在研究Robolectric但是发生了错误

时间:2021-01-05 00:25:22

I am studying Robolectric.

我正在研究Robolectric。

My project dir is, 我正在研究Robolectric但是发生了错误

我的项目目录是,

My unit test codes MainActivityTest are as follows,

我的单元测试代码MainActivityTest如下,

package com.example.activity;

import android.app.Activity;

import com.example.BuildConfig;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertTrue;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityTest {

  @Test
  public void titleIsCorrect() throws Exception {
    Activity activity = Robolectric.setupActivity(MainActivity.class);
    assertTrue(activity.getTitle().toString().equals("Deckard"));
  }
}

The configuration is, 我正在研究Robolectric但是发生了错误

配置是,

When I run the test code, 我正在研究Robolectric但是发生了错误

当我运行测试代码时

It goes wrong,

它出错了,

/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:51663,suspend=y,server=n -ea -javaagent:/Users/liren/Library/Caches/AndroidStudio1.2/groovyHotSwap/gragent.jar -Dfile.encoding=UTF-8 -classpath "/Applications/Android Studio 2.app/Contents/lib/idea_rt.jar:/Applications/Android Studio 2.app/Contents/plugins/junit/lib/junit-rt.jar:/Users/liren/Library/Android/sdk/platforms/android-19/data/res:/Users/liren/code/robolectric/RobolectricGo/android-api-19/build/intermediates/classes/test/debug:/Users/liren/code/robolectric/RobolectricGo/android-api-19/build/intermediates/classes/debug:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.apache.maven/maven-ant-tasks/2.1.3/b09be554228d66d208e5fef5266844aacf443abc/maven-ant-tasks-2.1.3.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.apache.ant/ant/1.8.0/7b456ca6b93900f96e58cc8371f03d90a9c1c8d1/ant-1.8.0.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.assertj/assertj-core/1.7.0/c0451c7fce61d14f9f0fa84bb2760e548221e471/assertj-core-1.7.0.jar:/Users/liren/.m2/repository/com/ibm/icu/icu4j/53.1/icu4j-53.1.jar:/Users/liren/.m2/repository/com/google/android/apps/common/testing/accessibility/framework/accessibility-test-framework/1.0/accessibility-test-framework-1.0.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.robolectric/robolectric-annotations/3.0-SNAPSHOT/4e48c5059ce3fae922863f3464fa05ce6a5e08d6/robolectric-annotations-3.0-SNAPSHOT.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.apache.ant/ant-launcher/1.8.0/8b53ba16fa62fb1034da8f1de200ddc407c8381/ant-launcher-1.8.0.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.ow2.asm/asm-commons/5.0.1/7b7147a390a93a14d2edfdcf3f7b0e87a0939c3e/asm-commons-5.0.1.jar:/Users/liren/.m2/repository/com/almworks/sqlite4java/sqlite4java/0.282/sqlite4java-0.282.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.ow2.asm/asm-tree/5.0.1/1b1e6e9d869acd704056d0a4223071a511c619e6/asm-tree-5.0.1.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.ow2.asm/asm/5.0.1/2fd56467a018aafe6ec6a73ccba520be4a7e1565/asm-5.0.1.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.ow2.asm/asm-analysis/5.0.1/e286fbee48efacb4e7c175f7948d9d8b2ab52352/asm-analysis-5.0.1.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.robolectric/robolectric/3.0-SNAPSHOT/fde4745244f1447557b2f55030cb4fc546b53fac/robolectric-3.0-SNAPSHOT.jar:/Users/liren/.m2/repository/com/ximpleware/vtd-xml/2.11/vtd-xml-2.11.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.ow2.asm/asm-util/5.0.1/7c8caddfbd0b2d7b844f8fcc75175b9cb9cf4724/asm-util-5.0.1.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.1/860340562250678d1a344907ac75754e259cdb14/hamcrest-core-1.1.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.bouncycastle/bcprov-jdk16/1.46/ce091790943599535cbb4de8ede84535b0c1260c/bcprov-jdk16-1.46.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.robolectric/robolectric-utils/3.0-SNAPSHOT/bf446c7a07ee47a454c8db9ad10c1a8ca2515fd9/robolectric-utils-3.0-SNAPSHOT.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/junit/junit/4.10/e4f1766ce7404a08f45d859fb9c226fc9e41a861/junit-4.10.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.robolectric/robolectric-resources/3.0-SNAPSHOT/ee905e9f06d0056f8d165a9a8ba24864825baa64/robolectric-resources-3.0-SNAPSHOT.jar:/Users/liren/kit/gradle-2.4/caches/modules-2/files-2.1/org.robolectric/shadows-core/3.0-SNAPSHOT/419067a26312d0bd71b00d81fa4bc501f7ccdabd/shadows-core-3.0-SNAPSHOT.jar:/Users/liren/code/robolectric/RobolectricGo/android-api-19/build/intermediates/mockable-android-19.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.example.activity.MainActivityTest
objc[13827]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Connected to the target VM, address: '127.0.0.1:51663', transport: 'socket'
java.io.FileNotFoundException: build/intermediates/bundles/debug/AndroidManifest.xml (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at org.robolectric.res.FileFsFile.getInputStream(FileFsFile.java:78)
    at org.robolectric.manifest.AndroidManifest.parseAndroidManifest(AndroidManifest.java:132)
    at org.robolectric.manifest.AndroidManifest.getTargetSdkVersion(AndroidManifest.java:485)
    at org.robolectric.RobolectricTestRunner.pickSdkVersion(RobolectricTestRunner.java:430)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:184)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:149)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    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)

java.lang.UnsupportedOperationException: Robolectric does not support API level 1.
    at org.robolectric.internal.SdkConfig.<init>(SdkConfig.java:38)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:184)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:149)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)

Disconnected from the target VM, address: '127.0.0.1:51663', transport: 'socket'

Process finished with exit code 255

It says something about my java. The java information is, 我正在研究Robolectric但是发生了错误

它讲述了我的java。 java信息是,

Why does it say

为什么这么说呢

One of the two will be used. Which one is undefined.

将使用两者之一。哪一个未定义。

The entire code is at my github RobolectricGo.

整个代码都在我的github RobolectricGo上。

2 个解决方案

#1


Looks like you are a Mac user. Did you try your project on command line?

看起来你是Mac用户。你在命令行上试过你的项目了吗?

There is a nice page with great information how to get started with robolectric: http://robolectric.org/getting-started/ and there you get the following hint.

有一个很好的页面,有很多信息如何开始使用robolectric:http://robolectric.org/getting-started/并且你得到以下提示。

Note for Mac Users

Mac用户请注意

If you are on a Mac, you will probably need to configure the default JUnit test runner configuration in order to work around a bug where Android Studio does not set the working directory to the module being tested. This can be accomplished by editing the run configurations, Defaults -> JUnit and changing the working directory value to $MODULE_DIR$.

如果您使用的是Mac,则可能需要配置默认的JUnit测试运行器配置,以便解决Android Studio未将工作目录设置为正在测试的模块的错误。这可以通过编辑运行配置,默认值 - > JUnit并将工作目录值更改为$ MODULE_DIR $来实现。

#2


Also try adding

也尝试添加

<uses-sdk android:targetSdkVersion="21" />

to your AndroidManifest.xml

到您的AndroidManifest.xml

and

@RunWith(RobolectricTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml")

to your test classes.

到你的考试班。

#1


Looks like you are a Mac user. Did you try your project on command line?

看起来你是Mac用户。你在命令行上试过你的项目了吗?

There is a nice page with great information how to get started with robolectric: http://robolectric.org/getting-started/ and there you get the following hint.

有一个很好的页面,有很多信息如何开始使用robolectric:http://robolectric.org/getting-started/并且你得到以下提示。

Note for Mac Users

Mac用户请注意

If you are on a Mac, you will probably need to configure the default JUnit test runner configuration in order to work around a bug where Android Studio does not set the working directory to the module being tested. This can be accomplished by editing the run configurations, Defaults -> JUnit and changing the working directory value to $MODULE_DIR$.

如果您使用的是Mac,则可能需要配置默认的JUnit测试运行器配置,以便解决Android Studio未将工作目录设置为正在测试的模块的错误。这可以通过编辑运行配置,默认值 - > JUnit并将工作目录值更改为$ MODULE_DIR $来实现。

#2


Also try adding

也尝试添加

<uses-sdk android:targetSdkVersion="21" />

to your AndroidManifest.xml

到您的AndroidManifest.xml

and

@RunWith(RobolectricTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml")

to your test classes.

到你的考试班。