如何在启用并行模式的情况下运行Android robolectric测试

Nic*_*ini 5 android maven robolectric android-gradle-plugin

我在干净的Gradle和Maven缓存上以并行模式运行Robolectric测试时遇到麻烦。

这是build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.robolectrictest.robotest"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    testOptions.unitTests.all {
        testLogging {
            events "failed", "skipped"
            showStackTraces = true
            exceptionFormat = "full"
        }
        maxParallelForks = 8
        forkEvery = 20
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0'
    androidTestCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)

在API 23上构建会显示相同的问题。如果我清除缓存(通过删除〜/ .gradle / caches /和〜/ .m2 /存储库)并运行

./gradlew clean test
Run Code Online (Sandbox Code Playgroud)

我遇到以下异常:

com.robolectrictest.robotest.ExampleUnitTest1 > addition_isCorrect1_1 FAILED
Unable to resolve artifact: Unable to get dependency information: Unable to read the metadata file for artifact 'org.robolectric:shadows-core:jar:21': Failed to build model from file '/Users/nicoloparolini/.m2/repository/org/robolectric/shadows-core/3.0/shadows-core-3.0.pom'.
        Error: 'no more data available - expected end tags </configuration></plugin></plugins></build></project> to close start tag <configuration> from line 131 and start tag <plugin> from line 128 and start tag <plugins> from line 119 and start tag <build> from line 105 and start tag <project> from line 2, parser stopped on TEXT seen ...</generatedSourcesDirectory>\n        </con... @133:14' for project org.robolectric:shadows-core
org.robolectric:shadows-core:jar:3.0

from the specified remote repositories:
sonatype (https://oss.sonatype.org/content/groups/public/),
        central (http://repo1.maven.org/maven2)

                Path to dependency:
                1) org.apache.maven:super-pom:pom:2.0

        Caused by:
        org.apache.maven.artifact.resolver.ArtifactResolutionException: Unable to get dependency information: Unable to read the metadata file for artifact 'org.robolectric:shadows-core:jar:21': Failed to build model from file '/Users/nicoloparolini/.m2/repository/org/robolectric/shadows-core/3.0/shadows-core-3.0.pom'.
        Error: 'no more data available - expected end tags </configuration></plugin></plugins></build></project> to close start tag <configuration> from line 131 and start tag <plugin> from line 128 and start tag <plugins> from line 119 and start tag <build> from line 105 and start tag <project> from line 2, parser stopped on TEXT seen ...</generatedSourcesDirectory>\n        </con... @133:14' for project org.robolectric:shadows-core
org.robolectric:shadows-core:jar:3.0

from the specified remote repositories:
        sonatype (https://oss.sonatype.org/content/groups/public/),
                central (http://repo1.maven.org/maven2)

                        Path to dependency:
                        1) org.apache.maven:super-pom:pom:2.0

                Caused by:
                org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException: Unable to read the metadata file for artifact 'org.robolectric:shadows-core:jar:21': Failed to build model from file '/Users/nicoloparolini/.m2/repository/org/robolectric/shadows-core/3.0/shadows-core-3.0.pom'.
        Error: 'no more data available - expected end tags </configuration></plugin></plugins></build></project> to close start tag <configuration> from line 131 and start tag <plugin> from line 128 and start tag <plugins> from line 119 and start tag <build> from line 105 and start tag <project> from line 2, parser stopped on TEXT seen ...</generatedSourcesDirectory>\n        </con... @133:14' for project org.robolectric:shadows-core

Caused by:
org.apache.maven.project.ProjectBuildingException: Failed to build model from file '/Users/nicoloparolini/.m2/repository/org/robolectric/shadows-core/3.0/shadows-core-3.0.pom'.
Error: 'no more data available - expected end tags </configuration></plugin></plugins></build></project> to close start tag <configuration> from line 131 and start tag <plugin> from line 128 and start tag <plugins> from line 119 and start tag <build> from line 105 and start tag <project> from line 2, parser stopped on TEXT seen ...</generatedSourcesDirectory>\n        </con... @133:14' for project org.robolectric:shadows-core

Caused by:
java.io.EOFException: no more data available - expected end tags </configuration></plugin></plugins></build></project> to close start tag <configuration> from line 131 and start tag <plugin> from line 128 and start tag <plugins> from line 119 and start tag <build> from line 105 and start tag <project> from line 2, parser stopped on TEXT seen ...</generatedSourcesDirectory>\n        </con... @133:14
Run Code Online (Sandbox Code Playgroud)

现在,文件在那里,格式正确,没有任何奇怪的特殊字符。看起来好像该过程在读取时已中断。

更多详细信息:这是正在测试的活动

package com.robolectrictest.robotest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final View button = findViewById(R.id.login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, MainActivity.class));
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

其布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/login"
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是测试,每个类重复10次,并触发200个类来触发多线程:

public class ExampleUnitTest1 {@Test
    public void addition_isCorrect1_1() throws Exception {
        MainActivity activity = Robolectric.setupActivity(MainActivity.class);
        activity.findViewById(R.id.login).performClick();

        Intent expectedIntent = new Intent(activity, MainActivity.class);
        Assert.assertNotSame(Shadows.shadowOf(activity).getNextStartedActivity(), expectedIntent);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在任何地方都找不到任何相关的错误或问题。有任何想法吗?