What do I need enable or install to get the annota

2019-07-08 21:09发布

问题:

I want to create a simple test with Robolectric for Android Apps using Android Studio.

Ok, it is my build.gradle on root directory:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

And it is my app build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    sourceSets {
        androidTest.setRoot('src/test')
    }

    defaultConfig {
        applicationId "com.example.robe.helloworld"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'

    testCompile 'org.robolectric:robolectric:3.0'
    testCompile 'junit:junit:4.12'
}

But when I try to invoke the @RunWith annotation, The IDE (AndroidStudio) doesn't find the annotation. So what do I need to configure, enable or install to get this feature ???

Info about Gradle

./gradlew -v

Gradle 2.4

Build time:   2015-05-05 08:09:24 UTC
Build number: none
Revision:     5c9c3bc20ca1c281ac7972643f1e2d190f2c943c

Groovy:       2.3.10
Ant:          Apache Ant(TM) version 1.9.4 compiled on April 29 2014
JVM:          1.8.0_60 (Oracle Corporation 25.60-b23)
OS:           Mac OS X 10.10.3 x86_64

UPDATE

In my test class I am trying to use the RunWith annotation in this way:

package com.example.robe.helloworld;

@RunWith()

public class WelcomeTest  {
}

But with Ctrl + space the IDE doesn't show me the annotation.

回答1:

It is already there:

testCompile 'junit:junit:4.12'

The only one thing to do in Android studio is to switch from instrumental tests to unit tests:



回答2:

It is in the Android test support library. Add the following to your dependencies:

androidTestCompile 'com.android.support.test:runner:0.3'