2014-05-10 103 views
5

我正在嘗試使用Gradle的Android Studio 0.5.8。配置Robolectric,Android Studio和Gradle進行測試應用

我現在的根構建文件:

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' 
     } 
    } 
} 

我的應用程序的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[email protected] == 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 cases 
java.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$6.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$1.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.<init>(JUnit4ClassRunner.java:37) 
     ... 39 more   

有關如何解決這個問題的任何想法?

回答

3

你不需要編寫自己的TestRunner。只要使用正確的命名方案爲您的測試應用程序類:TestNSApplication(而不是NSApplicationTest)

經常看見下面的模式:

  • *測試標誌着測試執行類
  • 測試*標誌着化妝類擴展一個類可測試

我希望你已經知道這個類必須與你的應用程序類具有相同的包。

1

好吧,顯然我解決它使用一個可定製的運行:

public class RobolectricGradleTestRunner extends RobolectricTestRunner { 
    public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError { 
     super(testClass); 
    } 

    @Override 
    protected Class<? extends TestLifecycle> getTestLifecycleClass() { 
     return MyTestLifecycle.class; 
    } 

    public static class MyTestLifecycle extends DefaultTestLifecycle { 
     @Override 
     public Application createApplication(final Method method, final AndroidManifest appManifest) { 
      return new TestNSApplication(); 
     } 
    } 
} 
相關問題