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;
}
}
You cannot use unit test for such code. You have two options:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.