robolectric的Maven依赖范围要么停止测试运​​行,要么使构建耗尽堆空间

时间:2013-05-31 06:56:13

标签: android maven robolectric

我有这个Android项目,并且使用Maven和amp;配置构建有些困难。 Robolectric。如果我将robolectric依赖的范围设置为'test',那么项目构建正常,但测试无法运行[junit或java.lang.NoClassDefFoundError的错误版本]。如果我没有设置robolectric依赖的范围,那么测试运行正常(并且通过)但是构建用尽了堆空间。

我是Maven的新手,我可能做错了。

我的依赖项看起来像这样::

    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>${platform.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.robolectric</groupId>
        <artifactId>robolectric</artifactId>
        <version>2.0-alpha-1</version>
        <scope>test</scope>
    </dependency>

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

    <dependency>
        <groupId>com.j256.ormlite</groupId>
        <artifactId>ormlite-android</artifactId>
        <version>4.45</version>
    </dependency>

    <dependency>
        <groupId>com.j256.ormlite</groupId>
        <artifactId>ormlite-core</artifactId>
        <version>4.45</version>
    </dependency>

    <dependency>
        <groupId>com.j256.ormlite</groupId>
        <artifactId>ormlite-jdbc</artifactId>
        <version>4.45</version>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.4</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.robolectric</groupId>
        <artifactId>robolectric</artifactId>
        <version>2.0-rc2</version>
        <scope>test</scope>
    </dependency>

</dependencies>

任何有关这里发生的事情的智慧都将被感激地收到并立即得到充分利用:)

这是使用Matt建议更新后的完整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>com.odl</groupId>
    <artifactId>GhostRunner</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>GhostRunner</name>

    <properties>
        <platform.version> 2.3.3 </platform.version>
        <android.sdk.path>/Users/chrisdanson/Development/android-sdk-macosx</android.sdk.path>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.robolectric</groupId>
            <artifactId>robolectric</artifactId>
            <version>2.1</version>
            <!--<scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-android</artifactId>
            <version>4.45</version>
        </dependency>

        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-core</artifactId>
            <version>4.45</version>
        </dependency>

        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-jdbc</artifactId>
            <version>4.45</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sdk>
                        <platform>10</platform>
                    </sdk>
                                        <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

所以..更新了插件依赖版本并运行了一个mvn'clead'&amp; “安装”以及问题仍然存在。运行robolectric测试会得到以下输出&gt;&gt;

java.lang.NoClassDefFoundError: org/objectweb/asm/Opcodes
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    at java.lang.Class.getConstructor0(Class.java:2699)
    at java.lang.Class.getConstructor(Class.java:1657)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:43)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Opcodes
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 30 more

进一步调查&gt;

link显示包含操作码的jar,我添加了此依赖项,我发现here

<dependency>
    <groupId>asm</groupId>
    <artifactId>asm-all</artifactId>
    <version>2.2</version>
</dependency>

我已将此添加为pom中的第一个依赖项,我仍然为OpCodes获取NoClassDefFound。

最后找到了似乎有效的东西::已经将<classifier>jar-with-dependencies</classifier>添加到robolectric依赖项中,最终运行了测试。

1 个答案:

答案 0 :(得分:0)

我看到你有

<dependency>
    <groupId>org.robolectric</groupId>
    <artifactId>robolectric</artifactId>
    <version>2.0-alpha-1</version>
    <scope>test</scope>
</dependency>

然后

<dependency>
    <groupId>org.robolectric</groupId>
    <artifactId>robolectric</artifactId>
    <version>2.0-rc2</version>
    <scope>test</scope>
</dependency>

我不相信你可以有两次相同的依赖列表,并且应用程序编译,但我可能错了。

尽管如此,请尝试更新一些依赖版本。我正在使用junit 4.10成功,以及robolectric 2.1(当我使用它时2.0也工作),然后mockito也是1.9.5。

我建议检查maven central以获取最新的依赖项。我相信之前有过jUnit 4.11和robolectric的问题,所以我还没有尝试将它升级到4.11。