PROFESSIONAL ANDROID
APP DEVELOPMENT
Testing
Why?
Reliable Software
Writing new code (new features or
refactoring) without having to worry over
breaking the pre-existing code
Testing Types
Black-box, used when we don’t know
implementation details.
• Acceptance testing
• System testing
Testing Types
White-box, when we do know the structure,
design and implementation.
• Unit testing
• Integration testing
Testing Types
How many tests?
Unit Tests
Small(contrary to end-to-end)
Many (literally, they have to be A LOT)
Testing different environments
Quick
Mocks
Simulated objects that imitate real
object’s behavior, in a controlled way.
Stubs
It allows us to isolate dependencies.
Mockito
A framework that allows us to write
test over a simple and
comprehensible API.
Tests are readable and with clear
verification errors.
Using Mockito
public class MockitoTest {
@Mock
private Dependecy dependency;
private TestSubject subject;
@Before
public void setUp() throws Exception {
[Link](this);
subject = new TestSubject(dependency);
}
@Test
public void testSomething() {
[Link]();
verify(dependecy).methodCalledInsideDoSomething();
}
}
Using Mockito
public class MockitoTest {
@Test
public void testSomehting(){
List mockedList = mock([Link]);
[Link]("one");
[Link]();
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
Using Mockito
public class MockitoTest {
@Test
public void testSomehting(){
LinkedList mockedList = mock([Link]);
when([Link](0)).thenReturn("first");
//first
[Link]([Link](0));
//null
[Link]([Link](999));
}
}
BDD
Given
When
Then
Given an initial context, when an
event occurs, then we ensure some
specific result.
BDD
Seller seller = mock([Link]);
Shop shop = new Shop(seller);
public void shouldBuyBread() throws Exception {
//given
given([Link]()).willReturn(new Bread());
//when
Goods goods = [Link]();
//then
assertThat(goods, containBread());
}
Mockito
Adding Support
[Link] file
dependencies {
testCompile '[Link]:mockito-all:1.10.19'
}
Unit testing on Android
Mockito is a testing framework for
Java, it cannot “imitate” the Android
component’s behaviour.
[Link]("Stub!
")
Robolectric
A unit test framework that allows the
execution of Android's JVM code. Uploads
Android classes on Java projects.
Using Robolectric
@RunWith([Link])
public class MyActivityTest {
@Test
public void clickingButton_shouldChangeResultsViewText() throws Exception {
MyActivity activity = [Link]([Link]);
Button button = (Button) [Link]([Link]);
TextView results = (TextView) [Link]([Link]);
[Link]();
assertThat([Link]().toString()).isEqualTo("Robolectric Rocks!");
}
}
Obtaining an Activity
Most of the time, this works
Activity activity = [Link]([Link]).create().get();
But we can also access other events within the
life cycle
ActivityController controller =
[Link]([Link]).create().start();
Activity activity = [Link]();
// revisar que algo no ha ocurrido
[Link]();
// revisar que ya ocurrió
Obtaining an Activity
Full Life-Cycle Creation
Activity activity =
[Link]([Link]).create().start().resume().visible
().get();
We can also simulate the calling Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
Activity activity =
[Link]([Link]).withIntent(intent).create().get()
;
Shadow classes
Modification or extension of real classes...
… But they are not proxies, fakes, mocks or
stubs
At least they’re not called sheeps :-)
Shadows
@Implements([Link])
public class ShadowImageView extends ShadowView {
...
@Implementation
public void setImageResource(int resId) {
// implementation here.
}
}
Accessing real objects
@Implements([Link])
public class ShadowPoint {
@RealObject private Point realPoint;
...
public void __constructor__(int x, int y) {
realPoint.x = x;
realPoint.y = y;
}
}
Robolectric
Adding support
[Link] file
dependencies {
compile '[Link]:robolectric:3.0'
}
How do I start?
Simplify the architecture (Or have one!)
Establish a test strategy
Take advantage of the mocks and stubs
Are there any other tools?
Many more
PowerMockito
Hamcrest
AssertJ Android
Robotium
UI Testing
Espresso
Firebase Test Lab
[Link]
More info:
• [Link]
• [Link]
• [Link]
c-shadows/shadows-core/src/main/java/org/robolectric/shadows
• [Link]
testing/[Link]
• [Link]