JUnit:具有私有字段的测试生成器
JUnit: test builder with private field
我是初学者,我在 class.
的构造函数中遇到 JUnit 测试问题
我要测试的class叫做IntSortedArray,如下:
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 创建了一个 class:
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);
}
}
我开始写一个测试class,目的是测试classIntSortedArray
的所有方法,包括构造函数。
第一种方法testConstructorArray16Elements()
要测试第一个builder。
所以我想我会检查数组元素的创建是否正确完成,所以 for 循环计算多长时间 elements
并确保它沿着 16(根据需要)。
但是 Eclipse 会(正确地)生成一个错误,因为 elements
是 private
。
我该如何解决这个错误?我不想放置 public
字段,如果可能的话,我想避免创建方法 public int[] getElements()
.
你有什么建议?
另外一个问题:我可以用同一个方法做两个assert吗?一个测试数组的长度,另一个测试 size
是否为 0.
希望不要犯大错,第一次使用JUnit
PS: 如何测试第二个构造函数?
非常感谢!
您必须为数组编写一个 getter 方法,或者实现一个迭代器
您的 class 字段似乎声明为私有,但您试图从 class 外部访问。您需要在 class 中提供 accessors 方法以使其可见:
private int[] elements;
private int size;
public static final int MAX = 16;
public int[] getElements() { ... }
public int getSize() { return size; }
那么你就可以写出下面的代码了:
isa = new IntSortedArray();
int expected = 0;
for(int i: isa.getElements()) {
expected += 1;
}
assertEquals(expected, IntSortedArray.MAX );
您的构造函数似乎为 16 个整数创建了一个数组,但没有用任何值对其进行初始化。为此,您应该使用以下代码:
public IntSortedArray() {
this.elements = new int[MAX];
this.size = 0;
for (int i=0 ; i < MAX ;i++) {
elements[i] = i;
size++;
}
}
我是初学者,我在 class.
的构造函数中遇到 JUnit 测试问题我要测试的class叫做IntSortedArray,如下:
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 创建了一个 class:
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);
}
}
我开始写一个测试class,目的是测试classIntSortedArray
的所有方法,包括构造函数。
第一种方法testConstructorArray16Elements()
要测试第一个builder。
所以我想我会检查数组元素的创建是否正确完成,所以 for 循环计算多长时间 elements
并确保它沿着 16(根据需要)。
但是 Eclipse 会(正确地)生成一个错误,因为 elements
是 private
。
我该如何解决这个错误?我不想放置 public
字段,如果可能的话,我想避免创建方法 public int[] getElements()
.
你有什么建议?
另外一个问题:我可以用同一个方法做两个assert吗?一个测试数组的长度,另一个测试 size
是否为 0.
希望不要犯大错,第一次使用JUnit
PS: 如何测试第二个构造函数?
非常感谢!
您必须为数组编写一个 getter 方法,或者实现一个迭代器
您的 class 字段似乎声明为私有,但您试图从 class 外部访问。您需要在 class 中提供 accessors 方法以使其可见:
private int[] elements;
private int size;
public static final int MAX = 16;
public int[] getElements() { ... }
public int getSize() { return size; }
那么你就可以写出下面的代码了:
isa = new IntSortedArray();
int expected = 0;
for(int i: isa.getElements()) {
expected += 1;
}
assertEquals(expected, IntSortedArray.MAX );
您的构造函数似乎为 16 个整数创建了一个数组,但没有用任何值对其进行初始化。为此,您应该使用以下代码:
public IntSortedArray() {
this.elements = new int[MAX];
this.size = 0;
for (int i=0 ; i < MAX ;i++) {
elements[i] = i;
size++;
}
}