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?
Asked
Active
Viewed 146 times
-1
-
Start here? https://developer.android.com/training/testing/start/index.html – OneCricketeer Apr 11 '17 at 11:29
2 Answers
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