简体   繁体   English

Java单元测试

[英]Java Unit Testing

I have a program and I am trying to write my Unit Test for it, I just need to know if it is correct? 我有一个程序,正在尝试为其编写单元测试,我只需要知道它是否正确? Here is the program: 这是程序:

public static int[] bucketSort(int[] entries)
    {
        int numberBuckets = maxVal(entries);
        //Creates an array with maxVal buckets. 
        int[] buckets = new int[numberBuckets+1];
        //Loop through input entries and add one to the count for every occurrence of that number in entries.
        for (int i = 0; i < entries.length; i++)
        {
            buckets[entries[i]]++; 
        }
           int key = 0;
           for (int i = 0; i < buckets.length; i++)
           {
            for (int j = 0; j < buckets[i]; j++)
            {
                //Use the number of every occurrence of each number in entries to construct the sorted array.
                entries[key] = i;
                key++;
            }
           } 
           //Print out the sorted array.
            for(int i = 0; i <= entries.length-1; i++)
            {
                System.out.print(entries[i] + ", ");
            }
           return entries;
    }

Here is what I have for the Unit Test 这是单元测试的内容

import junit.framework.Assert;
import junit.framework.TestCase;

        public void testBucketSort()
        {
            int[] a1 = {9, 6, 2, 2, 4, 1, 0, 10,};
            int[] a2 = BucketSort.bucketSort(a);
            int[] a3 = {0, 1, 2, 2, 4, 6, 9, 10,};
            Assert.assertArrayEquals(a2, a3);
        }
    }

No this is not correct. 不,这是不正确的。 There are two reasons: 有两个原因:

You have used already sorted data. 您已使用已排序的数据。 Though it should also be a test case, but you should use a random data set and apply sort on it. 尽管它也应该是一个测试用例,但是您应该使用随机数据集并对其进行排序。 Keep another array like a3 as your expected array and then call assertArrayEquals on a2 and a3. 保留另一个数组,如a3作为预期数组,然后在a2和a3上调用assertArrayEquals。

Your assertArrayEquals implementation is not verifying anything. 您的assertArrayEquals实现未验证任何内容。 Basically its just printing true or false but your test as such will always pass. 基本上,它只打印true或false,但是您的测试始终会通过。 You can use junit assertArrayEquals method from Assert class or right your own implementation where you should fail test if condition doesn't match. 您可以使用Assert类中的junit assertArrayEquals方法,也可以使用自己的实现,如果条件不匹配,则应通过失败测试。

Use JUnit, not your own testing method, unless you have a really good reason. 除非您有充分的理由,否则请使用JUnit,而不是您自己的测试方法。

Also, think of how your code could break and write a test for each case. 另外,请考虑一下您的代码可能如何中断并为每种情况编写测试。 example: 例:

  1. all in order 一切都井然有序
  2. reverse order 相反的顺序
  3. random order 随机顺序
  4. all duplicate values 所有重复值

Make sure in all cases the result is exactly the right length and sorted appropriately. 确保在所有情况下结果都正确正确的长度并正确排序。

The biggest problem that I see is that you're passing parameters to assertArrayEquals() in the wrong order: all JUnit assertions take parameters in the order expected , actual . 我看到的最大问题是,您以错误的顺序将参数传递给assertArrayEquals() :所有JUnit断言都按预期的 实际顺序接受参数。

Other than that, you don't have nearly enough tests. 除此之外,您没有足够的测试。 For any array-based method, I would writeat least 4 tests: one with 0 elements, one with 1, one with 2, and one with 3. This will catch most edge cases (0, 1, 2), and give at least some assurance that your code will handle increasing sizes gracefully (3). 对于任何基于数组的方法,我至少要编写4个测试:一个带有0个元素的测试,一个带有1个元素,一个带有2个元素,以及一个带有3个元素的测试。这将捕获大多数边缘情况(0、1、2),并且至少给出确保您的代码可以正常处理不断增加的大小(3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM