unit-testing - 使用随机数进行 Junit 测试

标签 unit-testing testing junit

我是测试新手。我正在使用 netbeans。

我有 junit 测试的大学作业,但我不太确定我应该如何测试随机数方法。 我的问题是我应该如何测试以下方法?

/**
     * Method will generate a set of integers between the minimum and maximum of
     * the requested size. If the uniqueElements is true the set will consist of
     * unique integer values
     *
     * @param size the number of elements for the array
     * @param minimum the lower value of the range of integers to generate
     * @param maximum the upper value of the range of integers to generate
     * @param uniqueElements flag for unique values
     * @return
     */
    public static ArrayList<Integer> createSet(int size, int minimum, int maximum, boolean uniqueElements) {
        boolean filled = false;
        int i = 0;
        ArrayList<Integer> arraySet = null;
        if (size > 0) {
            arraySet = new ArrayList<Integer>();
            boolean isUnique = false;
            while (!filled) {
                int randi = (int) (Math.random() * (maximum - minimum)) + minimum;
// C        isu = true;
// C       for (int j = 0; j < i && u; j++) {
// ** NEED &=  isu = randi != A.get(j);
// C       }
//
//        if (isu || !u) {
//          A.add(randi);
//          i++;
//        }
                isUnique = true;
                for (int j = 0; j < i && uniqueElements; j++) {
                    isUnique = randi != arraySet.get(j);
                }

                if (isUnique || !uniqueElements) {
                    arraySet.add(randi);
                    i++;
                }

                filled = (i == size);
            }
        }
        return arraySet;
    }

对于这个作业,教授告诉我覆盖 100% 的代码覆盖率。 我不确定我应该怎么做?

我创建了这个测试用例

/**
     * Test of createSet method, of class ArraySetUtilities.
     */
    @Test(timeout = 5000)
    public void testCreateSet() {

        System.out.println("createSet");
        int size = 0;
        int minimum = 0;
        int maximum = 0;
        boolean uniqueElements = true;
        ArrayList<Integer> expResult = null;
        ArrayList<Integer> result = ArraySetUtilities.createSet(size, minimum, maximum, uniqueElements);
        assertEquals(expResult, result);

    }

提前致谢

最佳答案

正如@flopshot 所说,您应该涵盖所有 IF。为此,您必须控制测试生成的随机数。

为此,我建议您将 Math.random() 替换为 SingletonRandom 并使用 JMockit 等模拟框架。

更改您的“生产”代码以使其可测试是必须仔细考虑的事情,但是,模拟随机是一个很好的方案。

您可以使用随机方法创建接口(interface)并实现它两次stack exchange question ,或创建一个类 SingletonRandom 并在您的测试中模拟它。这里描述了相同的概念 mock singleton with jmockit .

public class SingletonRandom {
   private SingletonRandom() {}

    public static SingletonRandom newInstance() {
        return new SingletonRandom();
    }

    public double getRandomNumber() {
        return Math.random();
    }
}

在你的类里面做类似的事情

int randi = (int) (SingletonRandom.newInstance().random() * (maximum - minimum)) + minimum;

并在测试中模拟 SingletonRandom

ClassTest {
    @Mocked
    SingletonRandom SingletonRandom;

    @Test
    testMethod() {
       new Expectations() {{
           SingletonGenerator.newInstance(); result = singletonGenerator;
           singletonGenerator.getRandomNumber(); result = new double[] { 1.2, 2.5, 3.7 };   // Add the requested "random" numbers for your if casses
       }};

       // Add calls to method with assertion here
    }
}

您可以在此处进一步阅读有关期望的信息 A Guide to JMockit Expectations

关于unit-testing - 使用随机数进行 Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53473656/

相关文章:

java - JPa/hibernate/JUnit : How to use a proper method of reflection in the ToString?

junit - spock:在任何测试类执行之前需要一个钩子(Hook)来执行一些设置步骤

angularjs - 对 AngularJS 指令的事件广播进行单元测试

testing - 测试计划/文档/管理工具

c# - 在 ASP.NET Core 1.1 中自动生成单元测试

javascript - 在nodejs单元测试中使用mocha处理不同的响应

Django 测试客户端意外重定向

spring-boot - 部署时 Maven 没有运行我的 mockito 测试

android - 模拟 Google Analytics v4

java - 特定对象列表的内存使用情况