GithubHelp home page GithubHelp logo

android-studio-robolectric-example's Introduction

build status

Android Studio Robolectric Test Example


This example project shows how to use robolectric, junit and assertJ with your gradle-based Android Studio projects. Examine the top-level build.gradle and app/build.gradle files for a new project configuration gradle boilerplate.

Make sure you're running the most recent version of Android Studio from the Canary Channel for this to work correctly (1.2 Beta as of today).

To set up from a new Android Studio Project:

Add test dependencies on assertj-android and Robolectric to your module's build.gradle file. Your app module's app/build.gradle file would look something like this:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.joshskeen.myapplication"
        minSdkVersion 16
        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'])
    testCompile 'junit:junit:4.12'
    testCompile 'com.squareup.assertj:assertj-android:1.1.0'
    testCompile 'org.robolectric:robolectric:3.0'
}
  1. If they don't already exist, create directories matching src/test/java/ and add a package matching your project's packagename. E.g. src/test/java/com.example.joshskeen.myapplication

  1. Sync your gradle file by clicking 'Sync Project with Gradle Files'

  1. Select "Unit Tests" under "Build Variants"

  1. Update default JUnit working directory. Select Run/Debug Configurations, then Defaults, then JUnit, then Configurations tab, then Working directory, and finally MODULE_DIR.: default JUnit working directory

  2. ctrl + click on the test and select Run MyActivityTest. run test menu item

  3. Create your unit test in src/test/java/com.example.joshskeen.myapplication/MyActivityTest.java:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class MyActivityTest {

    private MyActivity mActivity;

    @Before
    public void setup() {
        mActivity = Robolectric.buildActivity(MyActivity.class).create().get();
    }

    @Test
    public void myActivityAppearsAsExpectedInitially() {
        assertThat(mActivity.mClickMeButton).hasText("Click me!");
        assertThat(mActivity.mHelloWorldTextView).hasText("Hello world!");
    }

    @Test
    public void clickingClickMeButtonChangesHelloWorldText() {
        assertThat(mActivity.mHelloWorldTextView).hasText("Hello world!");
        mActivity.mClickMeButton.performClick();
        assertThat(mActivity.mHelloWorldTextView).hasText("HEY WORLD");
    }

}

Now write Robolectric tests! For more intel on how to write tests using robolectric + assertJ, check out http://blog.bignerdranch.com/2583-testing-the-android-way/

android-studio-robolectric-example's People

Contributors

jingibus avatar joshskeen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

android-studio-robolectric-example's Issues

org/sonatype/oss/oss-parent/7/oss-parent-7.pom missing

when executing test its fails with error:

Downloading: org/sonatype/oss/oss-parent/7/oss-parent-7.pom from repository sonatype at      https://oss.sonatype.org/content/groups/public/
Unable to locate resource in repository
[INFO] Unable to find resource 'org.sonatype.oss:oss-parent:pom:7' in repository sonatype (https://oss.sonatype.org/content/groups/public/)
Downloading: org/sonatype/oss/oss-parent/7/oss-parent-7.pom from repository central at    http://repo1.maven.org/maven2
Error transferring file: Connection refused: connect
[WARNING] Unable to get resource 'org.sonatype.oss:oss-parent:pom:7' from repository central (http://repo1.maven.org/maven2): Error transferring file: Connection refused: connect

and yes when looking up: https://oss.sonatype.org/content/groups/public/org/sonatype/oss/oss-parent/

there is no 7, only 8 and 9

Please show Run Configurations

Please show Run Configurations to configure it....because it doesn't run with Unit, and Gradle config you didn't show

Tests not running in Android Studio or command line

I opened the project and got it compiling without errors. However, when I run a test in the IDE, I get

Error: Could not find or load main class 2.app.build.intermediates.classes.debug:.Users.rakesh.AndroidStudioProjects.android-studio-robolectric-example-master

NoSuchMethod when running the test

Hi Josh !
First of all thanks by the example !
I m new to Roboletric and I m trying to run the example on Android Studio 1.2. The Android installations is new and there is no Roboletric plugins installed.
I just clone the project from Github, set the Build Variant to Unit Test, right click on the Project and run MyActivityTest. Before this I just resync the gradle build.
The problem is that the test throws a java.lang.NoSuchMethodException and I just can't find what is missing... Could you take a look on the error message ?

Console:

Testing started at 14:26 ...
14:26:58: Executing external tasks 'cleanTest test --tests com.example.joshskeen.myapplication.MyActivityTest'...
Configuration on demand is an incubating feature.:app:cleanTestUP-TO-DATE:app:preBuildUP-TO-DATE:app:preDebugBuildUP-TO-DATE:app:checkDebugManifest:app:prepareDebugDependencies:app:compileDebugAidlUP-TO-DATE:app:compileDebugRenderscriptUP-TO-DATE:app:generateDebugBuildConfigUP-TO-DATE:app:generateDebugAssetsUP-TO-DATE:app:mergeDebugAssetsUP-TO-DATE:app:generateDebugResValuesUP-TO-DATE:app:generateDebugResourcesUP-TO-DATE:app:mergeDebugResourcesUP-TO-DATE:app:processDebugManifestUP-TO-DATE:app:processDebugResourcesUP-TO-DATE:app:generateDebugSourcesUP-TO-DATE:app:compileDebugJava:app:preCompileDebugUnitTestJava:app:compileDebugUnitTestJava:app:compileDebugUnitTestSources:app:mockableAndroidJar:app:assembleDebugUnitTest:app:testDebugcom.example.joshskeen.myapplication.MyActivityTest > clickingClickMeButtonChangesHelloWorldText FAILED java.lang.RuntimeException
Caused by: java.lang.RuntimeException
Caused by: java.lang.NoSuchMethodException
com.example.joshskeen.myapplication.MyActivityTest > myActivityAppearsAsExpectedInitially FAILED java.lang.RuntimeException
Caused by: java.lang.RuntimeException
Caused by: java.lang.NoSuchMethodException
2 tests completed, 2 failed:app:testDebug FAILEDFAILURE: Build failed with an exception.* What went wrong:
Execution failed for task ':app:testDebug'.

There were failing tests. See the report at: file:///home/elcio/AndroidStudioProjects/android-studio-robolectric-example/app/build/reports/tests/debug/index.html

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    BUILD FAILEDTotal time: 10.662 secs
    14:27:11: External tasks execution finished 'cleanTest test --tests com.example.joshskeen.myapplication.MyActivityTest'.

Gladle build:

Executing tasks: [cleanTest, test]

Configuration on demand is an incubating feature.
:app:cleanTest UP-TO-DATE
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava
:app:preCompileDebugUnitTestJava
:app:compileDebugUnitTestJava
:app:compileDebugUnitTestSources
:app:mockableAndroidJar
:app:assembleDebugUnitTest
:app:testDebug

com.example.joshskeen.myapplication.MyActivityTest > clickingClickMeButtonChangesHelloWorldText FAILED
java.lang.RuntimeException
Caused by: java.lang.RuntimeException
Caused by: java.lang.NoSuchMethodException

com.example.joshskeen.myapplication.MyActivityTest > myActivityAppearsAsExpectedInitially FAILED
java.lang.RuntimeException
Caused by: java.lang.RuntimeException
Caused by: java.lang.NoSuchMethodException

2 tests completed, 2 failed

:app:testDebug FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:testDebug'.

    There were failing tests. See the report at: file:///home/elcio/AndroidStudioProjects/android-studio-robolectric-example/app/build/reports/tests/debug/index.html

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 10.662 secs

Thanks,

Elcio A.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.