Write and execute a local JVM unit test (Associate Android Developer Certification)

I found this example here: Google IO 2015 codelabs: Unit and UI Testing in Android Studio

Create Android Studio project. And add JUnit dependency to your gradle.build file(if there is not)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'

Go to Project perspective and create following folder(if there is not):


Then create following class:

public class Calculator {

    public double sum(double a, double b){
        return a+b;

    public double subtract(double a, double b){
        return a-b;

    public double divide(double a, double b){
        return a/b;

    public double multiply(double a, double b){
        return a*b;


Right click on your class name -> Go to -> Test -> Then select JUnit4 + Select setUp@Before


In order to run go to app/src/test/java/[your_package]/Run ‘Calculator Test’. Now you can see that ‘All 4 tests passed’.