使用“运行方式”运行JUnit测试与Maven测试之间有什么区别?

问题描述 投票:3回答:2

我有一些我不清楚的问题。我有一个名为ApplicationContext的类,该类扩展了android.app.Application。此类将由JMockIt的MockUp<T>嘲笑。当我让我的mvn install运行JUnit测试时,一切正常,但是当我使用Run As->JUnit Test运行测试时,我得到了这样的异常(仅此测试扩展了android.app.Application)...

java.lang.TypeNotPresentException: Type [unknown] not present

我想这与android必须在提供的范围内有关:

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

也许测试在运行时没有此软件包。但是我不知道为什么他们只在mvn install拥有它,因为该软件包始终以Maven依赖关系的形式链接。我需要了解为什么使用“运行方式”运行测试时找不到android.app.Application中的类。这是我的一些配置设置:

“构建路径”

。classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
    <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
    <classpathentry kind="src" output="bin/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" path="gen"/>
    <classpathentry kind="src" output="bin/classes" path="src/test/java">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="bin/classes"/>
</classpath>

。project

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>net.devgems.android.kurzparkzonewien-TRUNK</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ApkBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>

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>net.devgems.android</groupId>
    <artifactId>kurzparkzonewien</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>kurzparkzonewien</name>

    <properties>
        <platform.version>1.6_r2</platform.version>
        <android.sdk.path>/opt/android-sdk-linux</android.sdk.path>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>0.999.17</version>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>support-v4</artifactId>
            <version>r6</version>
        </dependency>
    </dependencies>

    <build>
        <outputDirectory>bin/classes</outputDirectory>
        <testOutputDirectory>bin/test-classes</testOutputDirectory>

        <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>4</platform>
                        <path>${android.sdk.path}</path>
                    </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>

如果您需要更多其他信息,请告诉我,因为我不确定在哪里可以正确找到问题所在。

android unit-testing maven m2e jmockit
2个回答
2
投票

我得出结论,我的问题中解释的错误是正确的行为。之所以如此,是因为您绝对不能期望Eclipse项目(或ADT插件)知道如何处理Maven组织的依赖关系。这是一个Maven项目,因此必须使用Maven进行安装,部署和测试。我认为“运行方式”和“ mvn测试”之间的区别在于,前者使用位于插件目录中的Eclipse内置的JUnit库,后者使用Maven配置的库。因此,这是两个不同的世界,它们没有共同之处。在我看来,Maven项目大多与Eclipse Run As等一起工作只是偶然。但是,如果存在依赖关系问题,则Eclipse无法对其进行管理,因为它是Maven项目而不是Eclipse项目。

但是对于上面的这种情况,我发现了一个很好的解决方法,该方法至少可以用于测试。我必须转到“ Java Build Path-> Libraries”,并且必须添加所有Maven依赖关系,包括使用的android.jar作为外部依赖关系(它不影响Maven)。然后,我可以通过“运行方式-> JUnit测试”执行所有测试(因此有时我必须首先执行“项目清理”,因为否则我会遇到ClassNotFoundExceptions)。

如果我的假设有误,请随时纠正我。


0
投票

我认为主要区别在于:

Run as Maven test将仅执行以“ Test”结尾的测试类。例如,如果测试类以“ Tests”或其他任何内容结束,则不会执行它们。但是Run as JUnit Test将测试所有类中的所有测试用例。

详细差异在下面的URL中列出:https://cwiki.apache.org/confluence/display/UIMA/Differences+between+Running+Unit+Tests+in+Eclipse+and+in+Maven

© www.soinside.com 2019 - 2024. All rights reserved.