-1

I have made a sample application in android and included the aar file in it and i have peform unit testing for the application whether it is possible to carry out unit testing for the sample application?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

Consider the below Sample Class for UnitTesting

public class SampleUnitTestClass {

    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }
}

After creating the class use the shortcut,Ctrl+Shift+T to create a new Test class corresponding to your sample Class.

  • Click on Create new Test
  • Select the methods that you want in your Unit test class and click ok(You could also change the Class name, destination package, Testing Library if needed)
  • Select destination directory and click OK
  • A Unit test class will be created

    public class SampleUnitTestClassTest {
    @Test
    public void add() throws Exception {
    
    }
    
    @Test
    public void subtract() throws Exception {
    
    }
    

    }

Write your testing logic here and asset your answer.For eg:

public class SampleUnitTestClassTest {
@Test
public void add() throws Exception {
    SampleUnitTestClass testClass = new SampleUnitTestClass();
    int answer = testClass.add(2,7);
    assertEquals("Addition of 2 positive integers",9,answer);
}

@Test
public void subtract() throws Exception {
    SampleUnitTestClass testClass = new SampleUnitTestClass();
    int answer = testClass.subtract(2,7);
    assertEquals("Subtraction of 2 positive integers",-5,answer);
}

}

Add more methods to include negative , null values etc and assert the answer.

remya thekkuvettil
  • 778
  • 1
  • 7
  • 22
0

For Unit Testing you can use Mockito and if u require some Android resources as well you can read about Robolectric.

Ashish
  • 183
  • 9