我对 Android 开发非常陌生,最近做了我的第一个项目。它只是一个基本的计算器,有加法、减法、乘法和除法。
现在,我希望使用 JUnits 和 KOTLIN 进行单元测试。我该怎么做呢?我查了一圈,还是不知道。
计算器 Java 代码:
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));
}
}
}
函数类代码:
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;
}
}
最佳答案
您不能对此类代码使用单元测试。 您有两个选择:
- 将计算器的业务逻辑提取到不相关的类中 Android框架并通过单元测试测试此类
- 使用 Instrumentation 测试和 Espresso 来测试 UI
第一个选项更可取(单元测试更快,并且您将拥有更好的架构)。
在这种情况下,使用 Java 或 Kotlin 进行测试没有区别,您应该使用与 Java 完全相同的方法和技术。
因此,您应该做的第一件事是查看 Android 开发者网站上的官方培训 - https://developer.android.com/training/testing/index.html
关于java - 使用 KOTLIN android studio 进行 JUnit 测试 [基本计算器],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44176585/