配置Robolectric,Android Studio和Gradle以获取测试应用程序

配置Robolectric,Android Studio和Gradle以获取测试应用程序,第1张

概述我正在尝试使用gradle的Android Studio 0.5.8.我当前的根构建文件:buildscript { repositories { mavenCentral() maven { url 'https://oss.sonatype.org/content/repositories/

我正在尝试使用gradle的Android Studio 0.5.8.

我当前的根构建文件:

buildscript {    repositorIEs {        mavenCentral()        maven {            url 'https://oss.sonatype.org/content/repositorIEs/snapshots'        }    }    dependencIEs {        classpath 'com.androID.tools.build:gradle:0.9.+'        classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'    }}allprojects {    repositorIEs {        mavenCentral()        maven {            url 'https://oss.sonatype.org/content/repositorIEs/snapshots'        }    }}

我的app build.gradle:

apply plugin: 'androID'apply plugin: 'robolectric'androID {    compileSdkVersion 19    buildToolsversion "19.0.3"    dependencIEs {        androIDTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'        androIDTestCompile 'org.mockito:mockito-all:1.9.5'        androIDTestCompile 'junit:junit:4.+'    }    defaultConfig {        minSdkVersion 9        targetSdkVersion 19        versionCode 1        versionname "1.0"    }    buildTypes {        release {            runProguard false            proguardfiles getDefaultProguardfile('proguard-androID.txt'),'proguard-rules.txt'        }    }}dependencIEs {    compile filetree(dir: 'libs',include: ['*.jar'])    compile 'com.androID.support:appcompat-v7:19.1.0'}task addTest {    def src = ['src/test/java']    def file = file("app.iml")    dolast {        try {            def parsedXml = (new XmlParser()).parse(file)            def node = parsedXml.component[1].content[0]            src.each {                def path = 'file://$MODulE_DIR$/' + "${it}"                def set = node.find { it.@url == path }                if (set == null) {                    new Node(node,'sourceFolder',['url': 'file://$MODulE_DIR$/' + "${it}",'isTestSource': "true"])                    def writer = new StringWriter()                    new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)                    file.text = writer.toString()                }            }        } catch (fileNotFoundException e) {            // nop,iml not found        }    }}gradle.projectsEvaluated {    preBuild.dependsOn(addTest)}tasks.withType(Test) {    scanForTestClasses = false    include "**/*Test.class"}

这是我的测试应用类:

public class NSApplicationTest extends NSApplication {    @OverrIDe    public Context getContext() {        return mock(Context.class);    }    @OverrIDe    public AlarmManager getAlarmManager() {        return mock(AlarmManager.class);    }}

这个配置的问题是任务addTest甚至加载NSApplicationTest类作为测试,并且它失败了:

   com.pack.scheduler.app.NSApplicationTest > initializationError Failed        java.lang.Exception  So I trIEd to change the name extension to see if this would fix everything but I'm still getting the same error. Then I trIEd with @Ignore on NSApplicationTest.Unable to load JUnit4 runner to calculate Ignored test casesjava.lang.reflect.InvocationTargetException        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)        at org.gradle.API.internal.tasks.testing.junit.AllExceptIgnoredTestRunnerBuilder$FallbackJUnit4Builder.runnerForClass(AllExceptIgnoredTestRunnerBuilder.java:52)        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)        at org.junit.internal.builders.AllDefaultPossibilitIEsBuilder.runnerForClass(AllDefaultPossibilitIEsBuilder.java:26)        at org.gradle.API.internal.tasks.testing.junit.IgnoredTestDescriptorProvIDer.getAllDescriptions(IgnoredTestDescriptorProvIDer.java:31)        at org.gradle.API.internal.tasks.testing.junit.JUnitTestEventAdapter.processIgnoredClass(JUnitTestEventAdapter.java:104)        at org.gradle.API.internal.tasks.testing.junit.JUnitTestEventAdapter.testIgnored(JUnitTestEventAdapter.java:92)        at org.junit.runner.notification.RunNotifIEr.notifyListener(RunNotifIEr.java:175)        at org.junit.runner.notification.RunNotifIEr$SafeNotifIEr.run(RunNotifIEr.java:61)        at org.junit.runner.notification.RunNotifIEr.fireTestIgnored(RunNotifIEr.java:172)        at org.junit.internal.builders.IgnoredClassRunner.run(IgnoredClassRunner.java:16)        at org.gradle.API.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)        at org.gradle.API.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)        at org.gradle.API.internal.tasks.testing.junit.JUnitTestClassprocessor.processtestClass(JUnitTestClassprocessor.java:69)        at org.gradle.API.internal.tasks.testing.SuiteTestClassprocessor.processtestClass(SuiteTestClassprocessor.java:50)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)        at java.lang.reflect.Method.invoke(Method.java:601)        at org.gradle.messaging.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:35)        at org.gradle.messaging.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:24)        at org.gradle.messaging.dispatch.ContextClassLoaderdispatch.dispatch(ContextClassLoaderdispatch.java:32)        at org.gradle.messaging.dispatch.ProxydispatchAdapter$dispatchingInvocationHandler.invoke(ProxydispatchAdapter.java:93)        at $Proxy2.processtestClass(UnkNown Source)        at org.gradle.API.internal.tasks.testing.worker.TestWorker.processtestClass(TestWorker.java:103)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)        at java.lang.reflect.Method.invoke(Method.java:601)        at org.gradle.messaging.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:35)        at org.gradle.messaging.dispatch.Reflectiondispatch.dispatch(Reflectiondispatch.java:24)        at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)        at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl.run(DefaultExecutorFactory.java:64)        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)        at java.lang.Thread.run(Thread.java:722)Caused by: org.junit.internal.runners.InitializationError        at org.junit.internal.runners.MethodValIDator.assertValID(MethodValIDator.java:57)        at org.junit.internal.runners.JUnit4ClassRunner.valIDate(JUnit4ClassRunner.java:47)        at org.junit.internal.runners.JUnit4ClassRunner.

关于如何解决这个问题的任何想法?最佳答案您不需要编写自己的TestRunner.只需为测试应用程序类使用正确的命名方案:TestNSApplication(而不是NSApplicationTest)

我经常看到以下模式:

> *测试标记测试执行类
> Test *标记类扩展以使类可测试

我希望您已经知道此类必须与您的应用程序类具有相同的包. 总结

以上是内存溢出为你收集整理的配置Robolectric,Android Studio和Gradle以获取测试应用程序全部内容,希望文章能够帮你解决配置Robolectric,Android Studio和Gradle以获取测试应用程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1140480.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)