Friday, May 6, 2016

Android Testing - JUnit Test

" Jai Saraswati Maa" 
Hi ... Dear All ..Today I am uploading a superb example of JUnit test.
JUnit test will test your code on JVM basis.
I had created one simple class NumAdder with a method addNumber which returns only addition of two numbers..

I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
Just Read Carefully ........ There is some configuration tips required.
**** No layout file required for this example.
**** Nothing added in manifest file or build gradle file so don't worry its very easy.

A. My Java class look like this -  in my package

package com.exam.ravi.junittestex1;

public class NumAdder
{
 public static  int addNumber(int first,int second)
 {
     return first+second;
 }
}

B. My Test class .... i am using default created class by AndroidStudio
which is ExampleUnitTest.java  in test folder(with same package name).

package com.exam.ravi.junittestex1;

import org.junit.Test;
import static org.junit.Assert.*;
public class ExampleUnitTest {
    @Test    public void addition_isCorrect() throws Exception
    {
        int result = NumAdder.addNumber(2,3);
        assertEquals(5, result);
    }
}

C. How configure ..... 


step 1. please select option Unit Tests in build variant see below 
        



step 2. Edit Run configuration  like 

step 3. Run ExampleUnitTest and see the result 

1. successful ------ see It's Green  
 

2. Fail ---- Please change expected result and actual result in ExampleUnitTest  
  

No comments:

Post a Comment