JUnit: private field test builder

I am a beginner and I have a problem with JUnit test in class constructor.

The class I want to test is called IntSortedArray and looks like this:

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...

}

      

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);
    }

}

      

I started writing a test class with the intention to test all methods of the class IntSortedArray

, including constructors.

The first method testConstructorArray16Elements()

wants to test the first builder. So I decided to check if the array elements were created correctly, so the for loop counts how long elements

and make sure it is 16 (as needed).

But Eclipse is throwing (correctly) an error because elements

- private

. How can I fix this error? I don't want to put the field public

, and if possible, I would like to avoid creating a method public int[] getElements()

.

What do you recommend?

Another question: can I make two assert the same method? One to check the length of the array and the other to check if it size

is 0.

I hope I didn't make any big mistakes, this is the first time I use JUnit.

PS: how can i check the second constructor?

Many thanks!

+2


source to share


2 answers


It looks like the fields of your class are being declared private, but you are trying to access them outside of the class. You need to provide accessors methods in your 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 can write below code:



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

      

It looks like your constructor created an array for 16 integers, but doesn't initialize it with any value. To do this, you must have the code below:

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

      

0


source


You will need to write a getter method for your array or implement an Iterator



-1


source







All Articles