用KOTLIN android studio进行JUnit测试

我对android开发很陌生,最近做了我的第一个项目。 这只是一个加法,减法,乘法和除法的基本计算器。

现在,我正在考虑使用JUnit和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; } 

}

你不能对这样的代码使用单元测试。 你有两个选择:

  1. 将计算器的业务逻辑提取到与Android框架无关的类并用单元测试来测试这个类
  2. 使用Instrumentation测试和Espresso来测试UI

第一个选项是更可取的(单元测试更快,你将有更好的架构)。

在这种情况下,使用Java或Kotlin进行测试没有区别,您应该使用与Java相同的方法和技术。

所以你应该做的第一件事就是检查Android Developer网站上的官方培训 – https://developer.android.com/training/testing/index.html