Pin Me

An Introduction to JUnits in the Android Development Environment

written by: Jbeerdev•edited by: Rebecca Scudder•updated: 5/7/2010

Anyone that writes software knows testing is a crucial part of the development process. Do you know how to do it in Android? Do you know what a JUnit is? In this article, we will introduce JUnits and begin explaining how to test your Android App with them.

  • slide 1 of 8

    Introduction – JUnit philosophy

    First of all, what its a JUnit? Well, we can use the xUnit term, to talk about all Testing frameworks, but we are working with Android, and with Java syntax, so we will use the term “JUnit" (the J is from Java).

    JUnits are a set of test classes that can be run in a controlled environment to test the functionality of a piece of our code or even a complete class.

    With this tool we can change or way to develop applications and it will help us to organize our code, create good APIs and even document the code we are testing on!

    But... all this means writing more code, doing more work!. How could this save us time?... The time you "spend" writing test classes for a specific functionality it will be saved from solving problems in that functionality. It is said that:

    "Per line of code, you have to write 10 lines of testing code"

    How can we do all this with a class that tests other classes?

    Let's have a look at it with a example; this is a standard JUnit example, applicable to Java and Android.

    Imagine that we are going to develop a calculator (This is the typical example when JUnits are explained, and the most clear to understand at the beginning. Later on in the Test-Series articles we will create more complex examples). I'm going to write the code using "generic pseudo code tests."

    To make this run in a full environment we need more elements, and this will be explained in the next articles.

  • slide 2 of 8

    Creating the first test

    When we are starting to write a code line, first we need to think how the code is going to test the functionality.

    What functionality do we need in our calculator? Addition, subtract, division and multiplication.

    So we are going to have a method for each functionality. But, as I said before, do not write any code lines of the Calculator.

    Let's think about the Addition functionality, and let's create a test for the “how it should work"

    Public void test_00AdditionTwoNumbers(){

    }

    Every JUnit test is a void method, with no input parameters and no returned values. The name of the test, in my point of view, should be the most representative as possible. Don't mind if the name is too long, the point is to understand what are you doing here.

    Now, let's initialize the variables we are going to use in the testing method

    int inputValue1 = 3

    int inputValue2 = 2

    int expectedValue = 5

    int actualValue = 0

    We are going to add the inputValue1 and the inputValue2, and we expect to receive from the calculator method (the method that has not been implemented yet.) the expectedValue (the number five).

    Now, the full implementation of the testing method:

    Calculator cal = new Calculator();

    actualValue = cal.add(inputValue1,inputValue2);

    Assert.assertEquals(expectedValue,actualValue);

    First we create the object cal from the class Calculator, then, we use the add method (This is the method under test) with the inputValue1 and the inputValue2 variables as inputs of the method. This method will return us back an integer value that we expect to be 5 (Adding 3 + 2). We store it in the actualValue variable (It's like saying: “Ok, this is the value I have from the 'add' method").

    The last line is very important in a xUnit test. Here we are using the Assert class (Java syntax) with the method assertEquals. This is the evaluation of the test. The test will pass or it will fail accordng to what happens in this line. Is our method doing what we expect it to do?

    As I have written above, if the expectedValue and actualValue are equals (assertEquals) then the test will pass.

    But... how will it pass if the class Calculator is not implemented? This code will cause compilation problems because the add method and the Calculator class are not created. So, let's create it now.

    public Calculator{

    public void Calculator(){

    }

    public int add(int inputValue1, int inputValue2){

    int result;

    result = inputValue1+inputValue2;

    return result;

    }

    }

    Very important!

    We create the method and the basic functionality to make the test pass. We are using only one test, but we can create more. (I recommend it. In this example it is not very useful, because it is very simple, but in further examples it will be necessary).

    Here are some examples of tests we can use with the “add" method:

    -What do we expect if we add two negative numbers?

    -What do we expect if we add a number and zero? (This seems stupid in this case, but imagine that we are testing the division method. What happens if the divisor is zero? We have to test all the possible cases and consequences.)

  • slide 3 of 8

    Testing in Android Development Environment

    In this series of articles I will write about testing philosophy, JUnits in Android and how to create useful testing classes.

    1. "An Introduction to JUnits in the Android Development Environment" by Jbeer
    2. "Junits in the Android Programming Environment" by Jbeer
    3. "Creating JUnits in Android" by Jbeer
  • slide 4 of 8
    Now we can set a number of testing-methods to test our functionality.
  • slide 5 of 8

    A complete set of tests

    Now that we have the general idea in our head of a “testing class," let's use it to test all the remaining methods.

    Remember: write the testing class before implementing the functionality. This way it can help you organize your mind and structure the “class under test" to create good quality code.

    Of the four methods we are going to implement, the most interesting for our testing examples is the division method.

    Let's think of a set of tests for this functionality:

    -What happens if we divide two integer numbers, like 4 and 2?

    -What happens if we divide the two integers above, but change the order?

    -What happens if we divide a integer by zero?

    -And if we divide zero by a integer number?

    This time, we will write 4 tests just for one functionality method.

    I propose you undertake the exercise of thinking of more tests for this method.

    Let's write them!

    test01_divisionWithTwoIntegersOrder1(){

    int intValue1 = 4;

    int intValue2 = 2;

    double expectedValue = 2.0;

    double actualValue = 0.0;

    Calculator cal = new Calculator();

    actualValue = cal.division(intValue1,intValue2);

    Assert.assertEquals(expectedValue,actualValue);

    }

    It's not necessary to create the “cal" object every time in every method. We can do it once, in a JUnit special method called “SetUp". But this will be explained in the other article, when the full environment is set.

    test02_divisionWithTwoIntegersOrder2(){

    int intValue1 = 2

    int intValue2 = 4;

    double expectedValue = 0.5;

    double actualValue = 0.0;

    Calculator cal = new Calculator();

    actualValue = cal.division(intValue1,intValue2);

    Assert.assertEquals(expectedValue,actualValue);

    }

    test03_divisionBetweenZero(){

    int intValue1 = 4;

    int intValue2 = 0

    Calculator cal = new Calculator();

    double actualValue = cal.division(intValue1,intValue2);

    Assert.fail():

    }

    Now, this testing case is interesting... because we are waiting a ZeroDivisionException in Java: demonstrating a failure in the functionality of the system we are testing.

    test04_divisionOfZeroAndIntegerNumber(){

    int intValue1 = 0

    int intValue2 = 4;

    double expectedValue = 0.0;

    double actualValue = 0.0;

    Calculator cal = new Calculator();

    actualValue = cal.division(intValue1,intValue2);

    Assert.assertEquals(expectedValue,actualValue);

    }

    Now we have the tests created, we have just have to create the functionality.

    In the next articles, I will write more about tests, with more complex examples. I will show you how to integrate the JUnits and the Android development environment.

  • slide 6 of 8

    Ask, comment, interact!

    Ask any questions you have about the article in comments, and I will try to answer as fast as I can. Comment my code, my writing, whether there may be something that is missing or is not complete; just let me know. In other words, interact!

  • slide 7 of 8

    Follow up

    If you want to know when new articles are released, subscribe to the Google Android channel RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev

  • slide 8 of 8

    Testing in Android Development Environment

    In this series of articles I will write about testing philosophy, JUnits in Android and how to create useful testing classes.

    1. "An Introduction to JUnits in the Android Development Environment" by Jbeer
    2. "Junits in the Android Programming Environment" by Jbeer
    3. "Creating JUnits in Android" by Jbeer