I am studying Robolectric but an error occured
Asked Answered
M

2

6

I am studying Robolectric.

My project dir is, enter image description here

My unit test codes MainActivityTest are as follows,

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, enter image description here

When I run the test code, enter image description here

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, enter image description here

Why does it say

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

The entire code is at my github RobolectricGo.

Mitzvah answered 7/5, 2015 at 4:13 Comment(0)
F
8

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

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.

Note for Mac Users

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$.

Felonious answered 7/5, 2015 at 5:31 Comment(1)
I had the same problem on Windows and this was the correct fix for me as well.Irving
Q
1

Also try adding

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

to your AndroidManifest.xml

and

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

to your test classes.

Quadragesima answered 9/7, 2015 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.