JUnit:具有私有字段的测试构建器 [英] JUnit: test builder with private field

查看:158
本文介绍了JUnit:具有私有字段的测试构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,我在类的构造函数中遇到JUnit测试问题。

I'm a beginner and I have a problem with JUnit test in the constructor of a class.

我要测试的类名为IntSortedArray,是如下:

The class that I want to test is called IntSortedArray and is as follows:

public class IntSortedArray {

    private int[] elements; 
    private int size; 


    public IntSortedArray() {
        this.elements = new int[16];
        this.size = 0;
    }

    public IntSortedArray(int initialCapacity) throws IllegalArgumentException {
        if(initialCapacity < 0) {
            throw new IllegalArgumentException("Error - You can't create an array of negative length.");
        }
        else {
            elements = new int[initialCapacity];
            size = 0;
        }
    }

    public IntSortedArray(int[] a) {
        elements = new int[a.length + 16];
        for(int i = 0; i < a.length; i++)
            elements[i] = a[i];
        size = a.length;
        insertionSort(elements);
    }

    //other code...

}

使用Eclipse我为JUnit创建了一个类:

With Eclipse I created a class for JUnit:

public class IntSortedArrayUnitTest {

    private IntSortedArray isa;

    @Test
    public void testConstructorArray16Elements() {
        isa = new IntSortedArray();
        int expected = 0;
        for(int i: isa.elements) **<-- ERROR**
         expected += 1;
        assertEquals(expected, 16);
    }

}

我开始写一个测试类打算测试类 IntSortedArray 的所有方法,包括构造函数。

I started to write a test class with the intention to test all the methods of the class IntSortedArray, including constructors.

第一个方法 testConstructorArray16Elements()想要测试第一个构建器。
所以我想我会检查数组元素的创建是否正确完成,因此for循环计算元素的长度,并确保它沿16(如必需)。

The first method testConstructorArray16Elements() wants to test the first builder. So I thought I would check if the creation of the array elements is done properly, so the for loop counts how long elements and make sure it along 16 (as required).

但Eclipse生成(正确)错误,因为元素 private
如何修复此错误?我不想放 public 字段,如果可能的话我想避免创建方法 public int [] getElements()

But Eclipse generates (rightly) a mistake because elements is private. How can I fix this error? I don't want to put the public field and if possible I would like to avoid creating a method public int[] getElements().

你推荐什么?

另一个问题:我可以做两个断言相同方法?一个用于测试数组的长度,另一个用于测试 size 为0。

Another question: I can do two assert the same method? One to test the length of the array and the other to test that size is 0.

我希望不要我犯了大错,这是我第一次使用JUnit。

I hope not to have made big mistakes, this is the first time I use JUnit.

PS:如何测试第二个构造函数?

PS: how can I test the second constructor?

非常感谢!

推荐答案

看起来您的类字段被声明为私有,但您尝试访问来自课外。您需要在类中提供访问者方法才能使它们可见:

It looks like your class fields are declare as private but you trying to access then from outside the class. You need to provide the accessors methods in you class to make them visible:

private int[] elements;
private int size; 
public static final int MAX = 16;

public int[] getElements() { ... }
public int getSize() { return size; }

然后你就可以写下面的代码:

Then you will be able to write below code:

isa = new IntSortedArray();
int expected = 0;
for(int i: isa.getElements()) {
  expected += 1;
}
assertEquals(expected, IntSortedArray.MAX );

看起来您的构造函数已经为16个整数创建了一个数组,但是没有用任何值初始化它。要做到这一点,你应该有以下代码:

It looks like your constructor has created an array for 16 integers, but does not initialize it with any value. To do that you should have below code:

public IntSortedArray() {
    this.elements = new int[MAX];
    this.size = 0;
    for (int i=0 ; i < MAX ;i++) {
       elements[i] = i;
       size++;
    }
}

这篇关于JUnit:具有私有字段的测试构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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