什么是创建一个JUnit测试案例的isSorted()方法的最佳方式? [英] What is the best way to create a JUnit test case for the isSorted() method?

查看:446
本文介绍了什么是创建一个JUnit测试案例的isSorted()方法的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是与测试 isSorted()方法所提供的JUnit测试案例的问题?该方法只检查int数组是按升序排列。我想彻底测试方法,但我不知道从哪里开始。例如:检查,如果数组为空或有中只有一个元素。鸭preciate任何帮助。

 公共类CheckArray {    / **
     *如果给定的数组按升序排序,则返回true。
     * /
    公共静态布尔isSorted(INT [] A){
        的for(int i = 0; I<则为a.length - 1;我++){
            如果(一个由[i]>将第[i + 1]){
                返回false;
            }
        }
        返回true;
    }
}公共类CheckArrayTest {
    @测试
    公共无效isSorted(){
        INT []数组= {2,1};
        CheckArray哈哈=新CheckArray(数组);
        assertFalse(haha.isSorted(NULL));
    }


解决方案

检查结果,如果数组为空或有一个元素是合理的测试。

另外,请查阅:


    以同样的元素
  • 2单元阵列

  • 不等元素在这两个订单
  • 2单元阵列

  • 与在开始,中间和结束位置的最大元素
  • 3元素的数组。

我不知道我会测试其他许多(也许是零投入的情况下)。最终,你只需要测试给你在它的正确性合理的信心;你可以添加更多的测试,而增加了信心。

另外,你可以组织你的测试在你的信念有关方法是如何工作的,例如


  • 如果我有一个数组,这个方法返回true,这是什么回报,如果我增加一个元素{小于,等于,大于}最后一个元素进行到底? (但愿假的,假的,真正的)

  • 如果我有一个数组,此方法返回false,这是什么回报,如果我增加一个元素{小于,等于,大于}最后一个元素进行到底? (在所有情况下,希望假)

What is the issue with the provided JUnit test case that tests the isSorted() method? The method simply checks if an array of ints is in ascending order. I am trying to thoroughly test the method but am not sure where to start. For example: check if array is empty or has only one element in it. Appreciate any help.

public class CheckArray {

    /**
     * Returns true if the given array is sorted in ascending order.
     */
    public static boolean isSorted(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] > a[i + 1]) {
                return false;
            }
        }
        return true;
    }
}

public class CheckArrayTest {
    @Test
    public void isSorted() {
        int[] array = { 2, 1 };
        CheckArray haha = new CheckArray(array);
        assertFalse(haha.isSorted(null));
    }

解决方案

Checking the result if the array is empty or has one element are reasonable tests.

Also check:

  • 2-element arrays with equal elements
  • 2 element arrays with unequal elements in both orders
  • 3 element arrays with the largest element at the beginning, middle and end position.

I'm not sure I'd test much else (maybe the null input case). Ultimately you just need the test to give you reasonable confidence in its correctness; you can add many more tests without increasing that confidence.

Alternatively, you can structure your tests around your belief about how the method works, e.g.

  • If I have an array for which this method returns true, what does it return if I add an element {less than, equal to, greater than} the last element to the end? (Hopefully false, false, true)
  • If I have an array for which this method returns false, what does it return if I add an element {less than, equal to, greater than} the last element to the end? (Hopefully false in all cases)

这篇关于什么是创建一个JUnit测试案例的isSorted()方法的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆