【问题标题】:JUnit testing with KOTLIN android studio [Basic Calculator]使用 KOTLIN android studio 进行 JUnit 测试 [基本计算器]
【发布时间】:2017-05-25 09:08:36
【问题描述】:

我对 android 开发非常陌生,最近做了我的第一个项目。它只是一个基本的计算器,有加减乘除。

现在,我希望通过 KOTLIN 使用 JUnit 进行单元测试。我该怎么做呢?我一直在四处寻找,不知道。

计算器 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;
}

}

【问题讨论】:

    标签: java android junit kotlin


    【解决方案1】:

    您不能对此类代码使用单元测试。 你有两个选择:

    1. 将计算器的业务逻辑提取到不相关的类中 Android 框架并使用单元测试测试此类
    2. 使用 Instrumentation 测试和 Espresso 测试 UI

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

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

    所以你应该做的第一件事是在 Android 开发者网站上查看官方培训 - https://developer.android.com/training/testing/index.html

    【讨论】:

    • 谢谢,我会去尝试第一个选项并回复你
    • 我已经将计算的业务逻辑提取到另一个java类。我可以使用 KOTLIN 对我的 Functions 类进行单元测试吗?我的项目要求我在 KOTLIN 中进行单元测试。现在,我将查看有关单元测试的教程,然后回到这里澄清我编辑原始帖子并添加 Functions 类代码的任何疑问
    • 附带说明,如果您出于业务原因未绑定到 jUnit,我强烈建议您查看 Kotlin 测试框架。我推荐两个主要的 - Spek(它有一些来自 Jetbrains 的贡献者,但不是官方的 Jetbrains 产品)和基于 Scalatest 的 Kotlintest。我在上一个工作地点介绍了后者,它对人们进行适当测试的意愿产生了巨大影响,仅仅是因为在没有 Java 样板的情况下创建实际测试变得更加简单和有趣。我希望 Kotlintest 或 Spek 有类似的效果。
    • 我实际上是被 JUnit 束缚的,并且需要使用 JUnit 来测试计算器的 Java 功能。我还需要使用 Espresso 来测试 UI。当我完成这个项目时,我会调查 Spek。希望它对我未来的任何项目都有帮助
    • 谢谢你们,gildor 和 Michael A。我现在已经完成了我的函数的单元测试,现在将查看 Espresso 的教程来测试 UI
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多