Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to JUnit and Robolectric

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Matt Tyndall Matt Tyndall
February 12, 2015

Introduction to JUnit and Robolectric

Resources:
Robolectric - http://robolectric.org/
Android Studio Gradle & Robolectric starter project - https://github.com/robolectric/deckard-gradle
Robolectric example projects - https://github.com/robolectric/robolectric-samples

Avatar for Matt Tyndall

Matt Tyndall

February 12, 2015
Tweet

Other Decks in Programming

Transcript

  1. @Test
 public void testShouldPass() {
 boolean expected = true;
 boolean

    actual = true;
 
 assertEquals(expected, actual);
 } @Test annotation Assertion we are testing
  2. @Test
 public void depositShouldUpdateAccountBalance() {
 double startingBalance = 100.00;
 BankAccount

    bankAccount = new BankAccount(startingBalance);
 
 double newDeposit = 20.00;
 bankAccount.depositAmount(newDeposit);
 
 double expectedBalance = 120.00;
 
 assertEquals(expectedBalance, bankAccount.getBalance());
 }
  3. public class MyClassTest {
 
 private BankAccount bankAccount;
 
 @Before


    public void setup() {
 double startingBalance = 100.00;
 bankAccount = new BankAccount(startingBalance);
 }
 
 @Test
 public void depositShouldUpdateAccountBalance() {
 double newDeposit = 20.00;
 bankAccount.depositAmount(newDeposit);
 
 double expectedBalance = 120.00;
 
 assertEquals(expectedBalance, bankAccount.getBalance());
 }
 
 @Test
 public void withdrawalShouldUpdateAccountBalance() {
 double newWithdrawal = 20.00;
 bankAccount.withdrawalAmount(newWithdrawal);
 
 double expectedBalance = 80.00;
 
 assertEquals(expectedBalance, bankAccount.getBalance());
 }
 } Using @Before
  4. Naming @Test
 public void multiplicationOfZeroShouldReturnZero() {
 …
 } public class

    NumberCruncherTest {
 …
 } add “Test” suffix to class name include “should” long names are cool
  5. Java source code Java Byte Code Java Byte Code !

    Java VM Java
 Compiler Dalvik Executable ! Dalvik VM Java source code Java Byte Code Java 
 Compiler Dex
 Compiler Dalvik Byte Code
  6. “your unit tests should only test your code and not

    depend on any particular behavior of the Android platform” Google says:
  7. Android Testing Framework by Google WACK Requires device or emulator

    Very slowwwww What about TDD Must build an .apk
  8. Run Android tests on JVM What does it do? Include

    Android classes in tests Tests are fast
  9. @RunWith(RobolectricTestRunner.class)
 public void MainActivityTest {
 
 @Test
 public void titleIsCorrect()

    {
 Activity activity = Robolectric.setupActivity(MainActivity.class);
 assertTrue(activity.getTitle().toString().equals(“Hello world”);
 }
 } Test runner Helper functions
  10. /** * Calls the same lifecycle methods on the Activity

    called by 
 * Android the first time the Activity is created. * * @return Activity controller instance. */ public ActivityController<T> setup() { return create().start().postCreate(null).resume().visible(); } Calls lifecycle methods for you
  11. @RunWith(RobolectricTestRunner.class) public class MyActivityTest { ! @Test public void clickingButton_shouldChangeResultsViewText()

    throws Exception { Activity activity = Robolectric.setupActivity(MyActivity.class); ! 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!")); } } Another example
  12. When would I use a shadow object? View state of

    private variable Example: AlertDialog How can I test title and message are correct? getTitle() and getMessage() are private ShadowAlertDialog dialogShadow = shadowOf(alert);
 assertThat(dialogShadow.getTitle().toString().equals(“Dialog title”);
  13. @Test public void shouldHaveALogo() throws Exception { ImageView pivotalLogo =

    (ImageView) activity.findViewById(R.id.pivotal_logo); ShadowImageView shadowPivotalLogo = Robolectric.shadowOf(pivotalLogo); assertThat(shadowPivotalLogo.resourceId, equalTo(R.drawable.pivotallabs_logo)); }
  14. Robolectric is not perfect Best available solution Only supports up

    to SDK 18 Random test failures initializationError
  15. Resources Robolectric http://robolectric.org/ Example project for configuring Gradle & Robolectric

    https://github.com/robolectric/deckard-gradle Robolectric project samples https://github.com/robolectric/robolectric-samples