by Wojtek Erbetowski
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
void testAggregateSevesUser() {
// 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
}
// expect
assertNotNull(title);
assertContains(title, "About");
expect:
title =~ 'About'
// ...
// then
assertNotNull(activity.getState());
assertNotNull(activity.getStatus());
assertNotNull(activity.getIcon());
assertNotNull(
activity.getEverythingElse()
);
// ...
expect:
activity."$field" != null
where:
field << [
"state", "status",
"icon", "everythingElse"
]
// given
User userMock = mock(User.class);
when(userMock.getEmail())
.thenReturn("email@email.com")
.thenReturn(null);
// ...
// then
verify(userMock, times(2)).getEmail()
given:
def userMock = Mock(User)
userMock.getEmail() >> [
'email@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>
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
// ..
}
public class MyTest {
public void test() {
Activity x = // Activity on the left loaded on MyTest bootstrap
getMyActivity(); // but the method returned shadowed activity
}
}
java.lang.ClassCastException: android.app.Activity
cannot be cast to android.app.Activity
class MyActivitySpec extends RoboSpecification {...}
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()
when:
taskActivity.onCreate(null)
then:
taskActivity.asyncText.text == "WebText"
}
}
@UseShadows(MyActivityManagerShadow)
class TaskActivityTestGroovy extends RoboSpecification {
def "should load text from async task"() {
given:
def taskActivity = new TaskActivity()
when:
taskActivity.onCreate(null)
then:
taskActivity.asyncText.text == "WebText"
}
}