问题描述
我必须确保robolectric-xxjar出现在android.jar之前,否则我会得到可怕的RuntimeException(Stub!)。
我该怎么做? 解决方案
我必须确保robolectric-xxjar出现在android.jar之前,否则我会得到可怕的RuntimeException(Stub!)。
我该怎么做? 解决方案
这是我完整的 build.gradle ,用于针对使用RoboGuice的Android应用程序运行Robolectric测试:
apply plugin:'java'
androidJar = new File(System.getenv('ANDROID_HOME'),'/ platforms / android-7 / android.jar')
配置{robo}
依赖关系{
robo('com.pivotallabs:robolectric:1.0-RC1')
testCompile ('org.roboguice:roboguice:1.1.2')
testCompile('junit:junit:4.8.2')
testCompile项目(':app')
testCompile文件(androidJar)
}
sourceSets.test.compileClasspath =配置。 robo + sourceSets.test.compileClasspath
sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath
test {
excludes = ['** / MyRobolectricTestRunner.class ']
}
我必须为测试运行器添加一个排除项,否则Gradle将会抛出一个异常。
package com.acme.myapp;
import java.io.File;
import org.junit.runners.model.InitializationError;
导入roboguice.application.RoboApplication;
导入roboguice.inject.ContextScope;
import com.google.inject.Injector;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;
public class MyRobolectricTestRunner扩展了RobolectricTestRunner {
public MyRobolectricTestRunner(Class<> testClass)throws InitializationError {
//告诉Robolectric在哪里可以找到AndroidManifest.xml和res /
super(testClass,new File(../ app));
}
/ **
*启用注入测试以及...
* /
@Override
public void prepareTest对象测试){
RoboApplication myApplication =(RoboApplication)Robolectric.application;
注射器注射器= 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;
导入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(只有部分股份(20,00 EUR)被接受,s);
}
}
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?
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);
}
}
这篇关于如何在Gradle中指定类路径排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!