24
Tests, Stubs, Mocks Effective Unittesting for android

Unit Testing Android Applications

Embed Size (px)

Citation preview

Page 1: Unit Testing Android Applications

Tests, Stubs, Mocks

Effective Unittesting for android

Page 2: Unit Testing Android Applications

Goals of test automation

Improve quality Understand the SUT Reduce the risk Easy to run Easy to write Easy to maintain

SubjectUnderTest

Page 3: Unit Testing Android Applications

Principles in test automation

Write the test first. Each test should be:

– Small and simple– Independent to other test– Repeatable– Self-checking Fully Automated

First do “State verification” and then “Behavior Verification”.

Page 4: Unit Testing Android Applications

Which part can be automated?

Page 5: Unit Testing Android Applications

System Under Test

It may have Depended-on components

Image From: xunit Test Patterns, G. Meszaros

Page 6: Unit Testing Android Applications

What are we talking about?

Pattern: Test Double Also known as: Imposter Problem: How can we verify logic

independently when code it depends on is unusable? How can we avoid Slow Tests?

Solution: We replace a component on which the SUT depends with a "test-specific equivalent."

Page 7: Unit Testing Android Applications

WHAT?

Test Doubles: Dummy Object: Temporary Object that does not influence the SUT Test Stub: Hand coded object used for testing Test Spy: Verification occurs after the test method has been called Mock Object: Expectations configured before calling tests Fake Object: Typically, it implements the same functionality as the real

DOC but in a much simpler way, no expectation are configured.

Page 8: Unit Testing Android Applications

Mock? Why? Mock - a simulated object that

mimics the behavior of a real object in controlled ways.

Page 9: Unit Testing Android Applications

Four phase testing

Image From: xunit Test Patterns, G. Meszaros

Page 10: Unit Testing Android Applications

Test structureSetup

Execute

Verify

Page 11: Unit Testing Android Applications

Effective test automation

After test generation by considering all paths and the features and organization specified, our test still may have these bad smells:– Slow Tests– Test Code Duplication– Obscure Tests– Buggy Tests

Page 12: Unit Testing Android Applications

Image SRC: www.dilbert.com

Page 13: Unit Testing Android Applications

Mockito, how to drink it? framework basics

import static org.mockito.Mockito.*; //mock creationLinkedList mockedList = mock(LinkedList.class);

//using mock objectmockedList.add("one");mockedList.clear();

//verificationverify(mockedList).add("one");verify(mockedList).clear();

Stub - Mockito can mock concrete classes, not only interfaces

Page 14: Unit Testing Android Applications

Mockito, how to drink it? framework basics

Return value thenReturn()when(mock.someMethod("some arg")).thenReturn("foo");

Stubbing voids requires doReturn() doReturn("bar").when(mock).foo();

Page 15: Unit Testing Android Applications

What else to use

PowerMock (private, final,static methods)

Jmockit (constructors and static methods mocking)

Hamcrest (library of matcher objects (also known as constraints or predicates) allowing 'match' rules to be defined declaratively)

Page 16: Unit Testing Android Applications

ANDROID UNIT TESTING

Page 17: Unit Testing Android Applications

EXAMPLE: JUNIT + ROBOLECTRIC for Android

Android unit testing is tricky:– android.jar only contains mocked out .class

files which leads to java.lang.RuntimeException: Stub!

TIPS:– Keep things simple by trying to make as many

services as possible not dependent on the parts of the Android platform that are not compatible with a conventional JVM.

– Robolectric to the rescue

Page 18: Unit Testing Android Applications

Unit test depending on Android api

@RunWith(RobolectricTestRunner.class)public class PopularRoutesAdapterTest { private PopularRoutesAdapter adapter;

@Before public void setUp() { adapter = new PopularRoutesAdapter(

Robolectric.buildActivity(Activity.class).create().get(); }

@Test public void testAddItem() { assertEquals(0, adapter.getCount()); adapter.addItem(new Route("Arnhem")); assertEquals(1, adapter.getCount()); }

Special test runner coming with Robolectric

Activity class is mocked by Robolectric

Page 19: Unit Testing Android Applications

General rules to remember

Mock it outside your code If you cannot test your code -> then probably

you should change it ;) cause its badly written

Test first Only one concrete class , mock the rest Only mock your neirest neighbour (Law of

Demeter -> dont talk with strangers) Think ;) and then write

Page 20: Unit Testing Android Applications

Robo-WTFRobolectric

Has Java implementations of class files for most of the Android API

Enables true unittesting, being not dependent on any network, hardware, device or database

Roboguice

Guice for RobolectricDependency Injection

based in Google GuiceCode bases DI instead

of XML (like in Spring)

Page 21: Unit Testing Android Applications

Deckard

Combination of – Gradle / Maven– Robolectric– Junit

Example adds– Roboguice– Mockito

Page 22: Unit Testing Android Applications

DEMO

Page 23: Unit Testing Android Applications

Developers Not Writing Tests

Symptoms:– No tests can be found when you ask to see the

• unit tests for a task,• customer tests for a User Story,

– Lack of clarity about what a user story or task really means Impact:

– Lack of safety net– Lack of focus

Possible Causes:– Hard to Test Code?– Not enough time?– Don’t have the skills?– Have been told not to?– Don’t see the value?

Robolectric for unittesting

Espresso for scenario-testing