如何在 Gradle 中指定类路径排序

发布于 2024-12-01 21:43:48 字数 140 浏览 1 评论 0原文

我需要控制 testRuntime 配置中 jar 的顺序。

我必须确保 robolectric-xxjar 出现在 android.jar 之前,否则我会得到可怕的 RuntimeException("Stub!")。

我该怎么做?

I need to control the ordering of jars in the testRuntime configuration.

I must make sure that robolectric-x.x.jar comes before android.jar, or else I get the dreaded RuntimeException("Stub!").

How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-12-08 21:43:48

这是我的完整 build.gradle,用于针对我的 Android 应用程序运行 Robolectric 测试,该应用程序使用 RoboGuice:

apply plugin: 'java'

androidJar = new File(System.getenv('ANDROID_HOME'), '/platforms/android-7/android.jar')

configurations { robo }

dependencies {
    robo('com.pivotallabs:robolectric:1.0-RC1')
    testCompile('org.roboguice:roboguice:1.1.2')
    testCompile('junit:junit:4.8.2')
    testCompile project (':app')
    testCompile files(androidJar)
}

sourceSets.test.compileClasspath = configurations.robo + sourceSets.test.compileClasspath
sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath

test {
    excludes = ['**/MyRobolectricTestRunner.class']
}

我必须为测试运行程序添加排除项,否则 Gradle 将引发异常。

MyRobolectricTestRunner.java 看起来像这样:

package com.acme.myapp;

import java.io.File;
import org.junit.runners.model.InitializationError;
import roboguice.application.RoboApplication;
import roboguice.inject.ContextScope;
import com.google.inject.Injector;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;

public class MyRobolectricTestRunner extends RobolectricTestRunner {
    public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError {
        // Tell Robolectric where to find AndroidManifest.xml and res/
        super(testClass, new File("../app"));
    }

    /**
     * Enable injection into tests as well...
     */
    @Override
    public void prepareTest(Object test) {
        RoboApplication myApplication = (RoboApplication) Robolectric.application;
        Injector injector = myApplication.getInjector();
        ContextScope contextScope = injector.getInstance(ContextScope.class);
        contextScope.enter(myApplication);
        injector.injectMembers(test);
    }

}

这是一个说明注入的示例测试:

package com.acme.myapp;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import roboguice.inject.InjectResource;

@RunWith(MyRobolectricTestRunner.class)
public class StringFormattingTest {

    @InjectResource(R.string.info_pending_amount)
    private String pendingAmountPattern;

    @Test
    public void testFormatInfoPendingAmount() {
        String s = String.format(pendingAmountPattern, 20.0d, "EUR");
        assertEquals("Only a part of your stake (20,00 EUR) was accepted", s);
    }

}

Here is my complete build.gradle for running Robolectric tests against my Android app, which uses RoboGuice:

apply plugin: 'java'

androidJar = new File(System.getenv('ANDROID_HOME'), '/platforms/android-7/android.jar')

configurations { robo }

dependencies {
    robo('com.pivotallabs:robolectric:1.0-RC1')
    testCompile('org.roboguice:roboguice:1.1.2')
    testCompile('junit:junit:4.8.2')
    testCompile project (':app')
    testCompile files(androidJar)
}

sourceSets.test.compileClasspath = configurations.robo + sourceSets.test.compileClasspath
sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath

test {
    excludes = ['**/MyRobolectricTestRunner.class']
}

I had to add an exclusion for the test runner, or else Gradle will throw an exception.

MyRobolectricTestRunner.java looks like this:

package com.acme.myapp;

import java.io.File;
import org.junit.runners.model.InitializationError;
import roboguice.application.RoboApplication;
import roboguice.inject.ContextScope;
import com.google.inject.Injector;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;

public class MyRobolectricTestRunner extends RobolectricTestRunner {
    public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError {
        // Tell Robolectric where to find AndroidManifest.xml and res/
        super(testClass, new File("../app"));
    }

    /**
     * Enable injection into tests as well...
     */
    @Override
    public void prepareTest(Object test) {
        RoboApplication myApplication = (RoboApplication) Robolectric.application;
        Injector injector = myApplication.getInjector();
        ContextScope contextScope = injector.getInstance(ContextScope.class);
        contextScope.enter(myApplication);
        injector.injectMembers(test);
    }

}

And here's a sample test that illustrates injection:

package com.acme.myapp;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import roboguice.inject.InjectResource;

@RunWith(MyRobolectricTestRunner.class)
public class StringFormattingTest {

    @InjectResource(R.string.info_pending_amount)
    private String pendingAmountPattern;

    @Test
    public void testFormatInfoPendingAmount() {
        String s = String.format(pendingAmountPattern, 20.0d, "EUR");
        assertEquals("Only a part of your stake (20,00 EUR) was accepted", s);
    }

}
清风夜微凉 2024-12-08 21:43:48

这可能有效:

configurations { robo }

dependencies {
  robo ...
  testRuntime ...
}

sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath

This might work:

configurations { robo }

dependencies {
  robo ...
  testRuntime ...
}

sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文