android - 警告(junit.framework.TestSuite $1): Exception in constructor when executing Android Test by Maven

标签 android maven junit android-testing

我通过 Maven 设置了一个 Android 测试项目,并按照 here 中的说明进行操作. 基本上我能够设置测试项目,甚至可以识别模拟器,但是在执行 mvn install(在 Eclipse 中)时我仍然无法通过 Maven 运行测试。如果我尝试执行它,我会收到以下错误:

<子> 失败的测试:警告(junit.framework.TestSuite $1):构造函数中的异常:testPersistAndRead(java.lang.RuntimeException: stub !(..) 警告(junit.framework.TestSuite $1):构造函数中的异常:testFileNotExists(java.lang.RuntimeException: stub !(..) 警告(junit.framework.TestSuite $1):构造函数中的异常:testCreateFiles(java.lang.RuntimeException:Stub!(..) 警告(junit.framework.TestSuite $1):构造函数中的异常:testFilesExist(java.lang.RuntimeException:Stub!(..) 警告(junit.framework.TestSuite $1):构造函数中的异常:testAndroidTestCaseSetupProperly(java.lang.RuntimeException:Stub!(..)

surfire 的输出告诉我它嵌套在 AndroidTestCase 中:

<子> junit.framework.AssertionFailedError:构造函数中的异常:testPersistAndRead(java.lang.RuntimeException: stub ! 在 android.test.AndroidTestCase.(AndroidTestCase.java:5) [...]

这是我尝试运行的测试。

public class PersistenceManagerTest extends AndroidTestCase {

    private final String FILENAME_PREFIX = "test.";

    protected void setUp() throws Exception {
        super.setUp();
        deleteInternalStorageFile();

        MockContentResolver resolver = new MockContentResolver();
        RenamingDelegatingContext renamingDelegatingContext = new RenamingDelegatingContext(new MockContext(), getContext(), FILENAME_PREFIX);
        Context context = new IsolatedContext(resolver, renamingDelegatingContext);

        setContext(context);
    }

    //AfterClass
    protected void tearDown() {
        deleteInternalStorageFile();
    }

    public void testPersistAndRead() throws IOException {
        String testData = "foobar";

        PersistenceManager.persist(testData, FileType.JSONDATA);

        String result = PersistenceManager.getFileData(FileType.JSONDATA);

        assertEquals(testData, result);
    }

    public void testFileNotExists()  {
        try {
            PersistenceManager.getFileData(FileType.JSONDATA);
            fail("PersistenceManager.getFileData should throw an exception!");
        } 
        catch (IOException e) {
            //expected
        }
    }

    public void testCreateFiles()  {
        try {
            PersistenceManager.createNewFiles();
        } 
        catch (IOException e) {
            fail("PersistenceManager.createNewFiles() threw an exception");
        }   
        try {
            PersistenceManager.getFileData(FileType.JSONDATA);
        } 
        catch (IOException e) {
            fail("PersistenceManager.getFileData should not throw an exception!");
        }
    }

    public void testFilesExist() throws IOException  {
        assertFalse(PersistenceManager.filesExist());

        PersistenceManager.createNewFiles();

        assertTrue(PersistenceManager.filesExist());
    }

    private void deleteInternalStorageFile() {
        getContext().deleteFile(PersistenceManager.JSONDATAFILE);
        getContext().deleteFile(PersistenceManager.TIMESTAMPFILE);
    }
}

...这是来自测试项目的 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.mydomain.android</groupId>
    <artifactId>myproject-androidtests</artifactId>
    <packaging>apk</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Testproject</name>

    <properties>
        <platform.version>2.1.2</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>
        <android.device>emulator</android.device>
    </properties>

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

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

        <dependency>
            <groupId>net.mydomain.android</groupId>
            <artifactId>myartifact</artifactId>
            <type>apk</type>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>net.mydomain.android</groupId>
            <artifactId>myartifact</artifactId>
            <scope>provided</scope>
            <type>jar</type>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>
    <build>
        <outputDirectory>bin/classes</outputDirectory>
        <testOutputDirectory>bin/test-classes</testOutputDirectory>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <sdk>
                        <path>${android.sdk.path}</path>
                        <platform>7</platform>
                    </sdk>
                </configuration>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

知道为什么这不起作用吗?

最佳答案

我找到了解决方案 here .

dtmilano 的回答基本上没有错,但我不知道这对我的测试意味着什么。解决方案是告诉 Maven 仪器测试不能作为 JUnit 测试运行,因此仪器测试不能在 src/test/java 中,而必须在 src/main/java 中。相反,它们必须包含在 apk 中,因此它们被部署到设备上并被触发运行。

关于android - 警告(junit.framework.TestSuite $1): Exception in constructor when executing Android Test by Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212396/

相关文章:

android - 在 Android 的 ImageView 中加载图像

maven-2 - 使用 Maven ant 任务将 jar 安装到本地存储库

java - 如何使用回调测试函数?

java - 我如何用mockito模拟 final方法

java - springboot读取tomcat-context.xml

android - “adb shell am set-inactive <packageName> true “does' t 工作

java - Android或iPhone开发有必要学习J2ME吗?

java - 如何运行已编译的java项目

java - 如何在 cocos2d android 1 中创建 cocos2d 粒子效果?

java - Maven war - 值为 'packaging' 的 'war' 无效。聚合器项目需要 'pom' 作为包装