0

I am very new to android developing and recently did my first project. It is only a basic calculator with addition, subtraction, multiplication and division.

Now, I am looking to go into Unit testing using JUnits with KOTLIN. How should I go about doing this? I've been searching around and have no idea.

Calculator Java Codes:

package com.example.zhiwen.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{

Button plus,minus,times,divide;
TextView textview3;
EditText first, second;
double no1 = 0, no2 = 0;
double answer = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    plus = (Button) findViewById(R.id.plus);
    minus = (Button) findViewById(R.id.minus);
    times = (Button) findViewById(R.id.times);
    divide = (Button) findViewById(R.id.divide);
    textview3 = (TextView) findViewById(R.id.textview3);
    first = (EditText) findViewById(R.id.editText);
    second = (EditText) findViewById(R.id.editText2);


}

public void ClickMeButton(View view){
    if(first.getText().toString().isEmpty() || second.getText().toString().isEmpty())
    {
        textview3.setText("Answer: -");
        Toast.makeText(getApplicationContext(),"Please fill up both numbers",Toast.LENGTH_SHORT).show();
    }
    else
    {
        no1 = Integer.parseInt(first.getText().toString());
        no2 = Integer.parseInt(second.getText().toString());
        textview3.setText("Answer: " + Functions.addFunction(no1,no2));
    }

}



public void ClickMeButton2(View view){
    if(first.getText().toString().isEmpty() || second.getText().toString().isEmpty())
    {
        textview3.setText("Answer: -");
        Toast.makeText(getApplicationContext(),"Please fill up both numbers",Toast.LENGTH_SHORT).show();
    }
    else
    {
        no1 = Integer.parseInt(first.getText().toString());
        no2 = Integer.parseInt(second.getText().toString());
        textview3.setText("Answer: " + Functions.minusFunction(no1,no2));
    }

}
public void ClickMeButton3(View view){
    if(first.getText().toString().isEmpty() || second.getText().toString().isEmpty())
    {
        textview3.setText("Answer: -");
        Toast.makeText(getApplicationContext(),"Please fill up both numbers",Toast.LENGTH_SHORT).show();
    }
    else
    {
        no1 = Integer.parseInt(first.getText().toString());
        no2 = Integer.parseInt(second.getText().toString());
        textview3.setText("Answer: " + Functions.multiFunction(no1,no2));
    }

}
public void ClickMeButton4(View view){
    if(first.getText().toString().isEmpty() || second.getText().toString().isEmpty())
    {
        textview3.setText("Answer: -");
        Toast.makeText(getApplicationContext(),"Please fill up both numbers",Toast.LENGTH_SHORT).show();
    }
    else
    {
        no1 = Integer.parseInt(first.getText().toString());
        no2 = Integer.parseInt(second.getText().toString());
        textview3.setText("Answer: " + Functions.divFunction(no1,no2));
    }

}

}

Function Class Codes:

package com.example.zhiwen.calculator;

/**
 * Created by Zhiwen on 5/25/2017.
 */

public class Functions {

public static double addFunction(double no1, double no2){
    double answer;
    answer = no1 + no2;
    return answer;
}

public static double minusFunction(double no1, double no2){
    double answer;
    answer = no1 - no2;
    return answer;
}

public static double multiFunction(double no1, double no2){
    double answer;
    answer = no1 * no2;
    return answer;
}

public static double divFunction(double no1, double no2){
    double answer;
    answer = no1 / no2;
    return answer;
}

}

1 Answer 1

2

You cannot use unit test for such code. You have two options:

  1. Extract business logic of calculator to class that not related to Android framework and test this class with unit tests
  2. Use Instrumentation tests and Espresso to test UI

The first option is much more preferable (Unit tests are faster and you will have much better architecture).

In this case, testing with Java or Kotlin has no difference, you should use exactly the same approaches and techniques like for Java.

So first thing that you should do is check official training on Android Developer website - https://developer.android.com/training/testing/index.html

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, I will go try out the first option and get back to you
I have extracted the business logic of the calculate to another java class. Will i be able to do unit testing on my Functions class with KOTLIN? My project requires me to do unit testing in KOTLIN. For now, I will go look into tutorials on unit testing and will get back here to clarify any doubts I have edited my original post and added the Functions class codes
As a side note, if you are not tied to jUnit for business reasons, I'd strongly recommend looking at a Kotlin testing framework. There are two main ones I'd recommend - Spek (which has some contributors from Jetbrains but is not an official Jetbrains product), and Kotlintest, which is based around Scalatest. I introduced the latter at my last place of work, and it made a huge difference in people's willngness to do proper testing simply because creating actual tests became so much simpler and more fun without Java boilerplate. I'd expect Kotlintest or Spek to have a similar effect.
I am actually tied down to JUnit and am required to use JUnit to test the Java functions of the calculator. I am also required to use Espresso to test UI. I will look into Spek when i am done with this project. Hopefully it will help with any of my future projects
Thank you to you both, gildor and Michael A. I am now done with the unit testing for my Functions and will now go look into tutorials for Espresso to test UI
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.