public class MainActivityFunctionalTest extends
ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity activity;
public MainActivityFunctionalTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
activity = getActivity();
}
public void testStartSecondActivity() throws Exception {
// add monitor to check for the second activity
ActivityMonitor monitor =
getInstrumentation().
addMonitor(SecondActivity.class.getName(), null, false);
// find button and click it
Button view = (Button) activity.findViewById(R.id.button1);
// TouchUtils handles the sync with the main thread internally
TouchUtils.clickView(this, view);
// to click on a click, e.g., in a listview
// listView.getChildAt(0);
// wait 2 seconds for the start of the activity
SecondActivity startedActivity = (SecondActivity) monitor
.waitForActivityWithTimeout(2000);
assertNotNull(startedActivity);
// search for the textView
TextView textView = (TextView) startedActivity.findViewById(R.id.resultText);
// check that the TextView is on the screen
ViewAsserts.assertOnScreen(startedActivity.getWindow().getDecorView(),
textView);
// validate the text on the TextView
assertEquals("Text incorrect", "Started", textView.getText().toString());
// press back and click again
this.sendKeys(KeyEvent.KEYCODE_BACK);
TouchUtils.clickView(this, view);
}
}
public class EditorTest extends
ActivityInstrumentationTestCase2<EditorActivity> {
private Solo solo;
public EditorTest() {
super(EditorActivity.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testPreferenceIsSaved() throws Exception {
solo.sendKey(Solo.MENU);
solo.clickOnText("More");
solo.clickOnText("Preferences");
solo.clickOnText("Edit File Extensions");
Assert.assertTrue(solo.searchText("rtf"));
solo.clickOnText("txt");
solo.clearEditText(2);
solo.enterText(2, "robotium");
solo.clickOnButton("Save");
solo.goBack();
solo.clickOnText("Edit File Extensions");
Assert.assertTrue(solo.searchText("application/robotium"));
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
}
MyProject/
AndroidManifest.xml
res/
... (resources for main application)
src/
... (source code for main application) ...
tests/
AndroidManifest.xml
res/
... (resources for tests)
src/
... (source code for tests)
MyApplicationProject
└── MyApplication
└── src
├── instrumentTest
│ └── java
└── main
├── java
└── res
// Test class for MyActivity
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
@Test
public void clickingButton_shouldChangeResultsViewText() throws Exception {
Activity activity = Robolectric.buildActivity(MyActivity.class).create().get();
Button pressMeButton = (Button) activity.findViewById(R.id.press_me_button);
TextView results = (TextView) activity.findViewById(R.id.results_text_view);
pressMeButton.performClick();
String resultsText = results.getText().toString();
assertThat(resultsText, equalTo("Testing Android Rocks!"));
}
}
@Implements(AlphaAnimation.class)
public class ShadowAlphaAnimation extends ShadowAnimation {
private float fromAlpha;
private float toAlpha;
public void __constructor__(float fromAlpha, float toAlpha) {
this.fromAlpha = fromAlpha;
this.toAlpha = toAlpha;
}
public float getFromAlpha() {
return fromAlpha;
}
public float getToAlpha() {
return toAlpha;
}
}
@Test
void schouldAggregateSevesUser() {
// given
User user = new User();
// when
aggregate.store(user);
// then
assertEquals(
user,
aggregate.findOnly()
);
}
def 'aggregate should save user'() {
given:
def user = new User()
when:
aggregate.store user
then:
aggregate.findOnly() == user
}
// ...
// then,
assertEquals(sum(3, 5), 8);
assertEquals(sum(1, 5), 6);
assertEquals(sum(4, 5), 9);
assertEquals(sum(5, 3), 8);
// ...
expect:
sum(a, b) == c
where:
a | b || c
3 | 5 || 8
1 | 5 || 6
4 | 5 || 9
5 | 3 || 8
// given
User userMock = mock(User.class);
when(userMock.getEmail())
.thenReturn("me@email.com")
.thenReturn(null);
// ...
// then
verify(userMock, times(2)).getEmail()
given:
def userMock = Mock(User)
userMock.getEmail() >> ['me@email.com', null]
// ...
then:
2 * userMock.getEmail()
@Test(expect=RuntimeException.class)
public void myTest() {
thisThrowsSomething();
}
when:
thisThrowsSomething()
then:
thrown(RuntimeException)
@Test
public void myTest() {
try {
thisThrowsSomething();
fail();
} catch(RuntimeException e) {
assertContains(
e.getMessage(), 'No such user')
}
}
when:
thisThrowsSomething()
then:
def e = thrown(RuntimeException)
e.message =~ 'No such user'
then:
userStorage.getAllUsers().find{it.id == id}?.name == "Simon"
Condition not satisfied:
userRepository.findAll().find{it.name == 'Simon'}?.age == 10
| | | | |
| | null | false
| [A$User(Adam, 12)] null
A$UserRepository@22d3d11f
<Click to see difference>
.
├── app
│ ├── build.gradle
│ ├── debug.keystore
│ ├── local.properties
│ └── src
│ ├── instrumentTest
│ ├── main
│ └── release
├── robospock
│ ├── build.gradle
│ └── src
│ └── test
└── settings.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
classpath 'com.novoda:gradle-android-test-plugin:0.9.1-SNAPSHOT'
}
}
apply plugin: 'groovy'
apply plugin: 'android-test'
repositories {
mavenCentral()
}
dependencies {
testCompile 'pl.polidea:RoboSpock:0.4'
testCompile rootProject
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.android.support:support-v4:13.0.0'
debugCompile 'com.android.support:support-v13:13.0.0'
compile 'com.google.android.gms:play-services:3.1.36'
}
android {
compileSdkVersion 15
buildToolsVersion "17.0"
testBuildType "debug"
signingConfigs {
myConfig {
storeFile file("debug.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
defaultConfig {
versionCode 12
versionName "2.0"
minSdkVersion 16
targetSdkVersion 16
}
buildTypes {
debug {
packageNameSuffix ".debug"
signingConfig signingConfigs.myConfig
}
}
}
def "should throw SQL Constraint exception on existing primary key"() {
given:
def dao = databaseHelper.getDao(DatabaseObject)
and: 'stored object'
def dbObject = new DatabaseObject("test", 4, 1)
dao.create(dbObject)
when: 'duplication'
dao.create(dbObject)
then:
def exception = thrown(RuntimeException)
exception.message =~ "SQLITE_CONSTRAINT"
exception.cause.class == SQLException
}
class TaskActivityTestGroovy extends RoboSpecification { @Inject WebInterface webInterface def setup() { inject { install(TestTaskExecutorModule) bind(WebInterface).toInstance(Mock(WebInterface)) } } def "should load text from async task"() { given: def taskActivity = new TaskActivity()
webInterface.getMainPageText() >> "WebText" when: taskActivity.onCreate(null) then: taskActivity.asyncText.text == "WebText" } }