Skip to content

Instantly share code, notes, and snippets.

@fatorx
Last active October 8, 2015 15:33
Show Gist options
  • Save fatorx/7fdbd71ae3824af43052 to your computer and use it in GitHub Desktop.
Save fatorx/7fdbd71ae3824af43052 to your computer and use it in GitHub Desktop.
Configuração Roboletric (Android)
Configuração Roboletric (http://robolectric.org)
build.gradle - (padrão)
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
testCompile 'com.android.support:support-annotations:23.0.1'
testCompile 'com.squareup.assertj:assertj-android:1.1.0'
testCompile "org.robolectric:robolectric:3.1-SNAPSHOT"
}
Em Build Variantes, na lateral
Test Artifact: Unit Tests
Depois seguir Config Unit Tests Android Studio (https://gist.github.com/fatorx/794b0fefa79d9a8998cd)
Testes:
package fatorx.com.br.myapplication;
import android.os.Build;
import android.widget.TextView;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(RobolectricGradleTestRunner.class)
@Config(manifest=Config.NONE, constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public class MainActivitySettersTest {
MainActivity activity;
@Before
public void setUp() {
activity = Robolectric.setupActivity(MainActivity.class);
}
@Test
public void clickingButton_shouldChangeResultsViewText() throws Exception {
activity = Robolectric.setupActivity(MainActivity.class);
//Button button = (Button) activity.findViewById(R.id.button);
TextView results = (TextView) activity.findViewById(R.id.results);
//button.performClick();
assertThat(results.getText().toString()).isEqualTo("Robolectric Rocks!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment