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)
public class ScanExampleTest
extends ActivityInstrumentationTestCase2<MyScanActivity> {
private Solo solo;
public ScanExampleTest() {
super("org.my.scanExample", MyScanActivity.class);
}
@Override
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testManualEntry() throws Exception {
solo.assertCurrentActivity("expecting example app", MyScanActivity.class);
boolean ableToScan = CardIOActivity.canReadCardWithCamera(solo.getCurrentActivity());
solo.clickOnButton(0);
if (ableToScan) {
// some devices don't support use of the camera.
solo.assertCurrentActivity("Expected CardIOActivity (scan)", "CardIOActivity");
solo.clickOnButton("Keyboard...");
}
solo.assertCurrentActivity("Expected DataEntryActivity", "DataEntryActivity");
solo.enterText(0, "4111111111111111");
solo.enterText(1, "12/22");
solo.clickOnButton("Done");
solo.assertCurrentActivity("Expected completion", MyScanActivity.class);
assertEquals("Card Number not found", true, solo.searchText("Card Number:"));
assertEquals("Expiry not found", true, solo.searchText("Expiration Date: 12/2022"));
}
}
Based on https://github.com/card-io/android-scan-demo
// 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 == "Szymon"
Condition not satisfied:
userRepository.findAll().find{it.name == 'Szymon'}?.age == 10
| | | | |
| | null | false
| [A$User(Piotr, 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" } }