Maven + Robolectric,找不到资源?

时间:2012-07-12 16:09:59

标签: java maven robolectric

我现在正在为Android编写一个演示应用程序,使用robolectric进行TA。我可以运行该项目,但是当我添加测试时,它会显示如下错误:

error message="java.lang.RuntimeException: no such directory /Users/majie/Downloads/android-sdk-macosx/platforms/android-9/data/res/values" type="java.lang.RuntimeException">java.lang.RuntimeException: java.lang.RuntimeException: no such directory /Users/majie/Downloads/android-sdk-macosx/platforms/android-9/data/res/values
at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:93)
at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:261)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:73)
at android.app.Activity.setContentView(Activity.java)
at com.cisco.jabberim.ui.WelcomeActivity.onCreate(WelcomeActivity.java:21)
at Test.FTE.WelcomeActivityTest.setUp(WelcomeActivityTest.java:28)

测试文件如下:

@RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest {
private WelcomeActivity mActivity;
private TextView text;

@Before
public void setUp(){
    mActivity = new WelcomeActivity();
    mActivity.onCreate(null);
    text = (TextView) mActivity.findViewById(R.id.welcome);
}

@Test
public void textIsNotNull(){
    String str = text.getText().toString();
    assertNotNull(str);
}
}

pom.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-    v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>im</groupId>
<artifactId>Im</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>IM demo</name>

<dependencies>
    <dependency>
        <groupId>android</groupId>
        <artifactId>android</artifactId>
        <version>3.0_r2</version>
        <scope>provided</scope>
    </dependency>

    <!-- Make sure this is below the android dependencies -->
    <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>1.0-RC1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>

    <plugins>
        <plugin>
            <!-- See http://code.google.com/p/maven-android-plugin/ -->
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <sdk>
                    <!-- platform or api level (api level 8 = platform 2.2)-->
                    <platform>11</platform>
                </sdk>        
            </configuration>
            <extensions>true</extensions>
        </plugin>
    </plugins>
</build>

我真的不知道如何解决它。我不知道为什么奇怪的路径是“/ Users / majie / Downloads / android-sdk-macosx / platforms / android-9 / data / res / values”。我不知道在哪里设置它或如何使它正确。

3 个答案:

答案 0 :(得分:0)

我在ResourceLoader源中找到了

private String getAndroidResourcePathFromSystemEnvironment() {
   // Hand tested
  String resourcePath = System.getenv().get("ANDROID_HOME");
  if (resourcePath != null) {
        return new File(resourcePath, getAndroidResourceSubPath()).toString();
  }
  return null;
}

所以我认为你必须尝试使用​​ANDROID_HOME环境变量。

答案 1 :(得分:0)

使用版本1.0的robolectric jar(而不是RC1)为我解决了这个问题

 <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>1.0</version>
        <scope>test</scope>
 </dependency>

答案 2 :(得分:-2)

这是Robolectric需要/ Users / majie / Downloads / android-sdk-macosx / platforms / android-9 / data / res / values但实际上你的计算机中不存在。

这很容易解决 只需下载android-9 sdk或复制其他sdk如android-8,并用android-9重命名

然后就可以了。