4

正確に理解できない問題があります。android.app.Application を拡張する ApplicationContext というクラスがあります。このクラスは、JMockIt の によってモックされますMockUp<T>。JUnit テストを で実行するmvn installと、すべて正常に動作しますが、 でテストを実行すると、Run As->JUnit Testこのような例外が発生します (android.app.Application を拡張するこの 1 つのテストでのみ)...

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

これは、アンドロイドが提供されたスコープ内になければならないという事実と関係があると思います:

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

実行時にテストにこのパッケージが含まれていない可能性があります。mvn installしかし、パッケージは常にMaven依存関係としてリンクされているため、なぜ彼らはそれをただ持っているのだろうか。「Run As」でテストを実行すると、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>

。事業

<?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>

さらに追加情報が必要な場合は、私に教えてください。問題を適切に特定する場所がわかりません。

4

2 に答える 2

2

私の質問で説明されているエラーは適切な動作であるという結論に達しました。これは、Eclipse (または ADT プラグイン) が Maven によって編成された依存関係を処理する方法を知っていることを Eclipse プロジェクトから期待してはならないためです。これは Maven プロジェクトであるため、インストール、デプロイ、およびテストには Maven を使用する必要があります。「Run As」と「mvn test」の違いは、最初はプラグインディレクトリにあるEclipseの組み込みJUnitライブラリを使用し、後者はMavenによって構成されたライブラリを使用することだと思います。したがって、これらは 2 つの異なる世界であり、共通点はありません。私の意見では、Maven プロジェクトのほとんどが Eclipse Run As などで動作するのは単なる偶然です。ただし、依存関係の問題がある場合、Eclipse プロジェクトではなく Maven プロジェクトであるため、Eclipse はそれを管理できません。

しかし、上記のこのケースでは、少なくともテストのために機能する素晴らしい回避策を見つけました。「Java Build Path -> Libraries」に移動する必要があり、使用されている android.jar を含むすべての Maven 依存関係を外部依存関係として追加する必要がありました (Maven には影響しません)。その後、「Run As -> JUnit Test」ですべてのテストを実行することができました (そうしないと ClassNotFoundExceptions に遭遇するため、最初に「Project Clean」を実行する必要がある場合があります)。

私の仮定が間違っている場合は、遠慮なく訂正してください。

于 2012-10-29T18:16:49.127 に答える