Yoey
Yoey

Reputation: 408

Simple JUnit test not working

We are currently learning JUnit testing in our programming class. Our task was to write a method that calculates the sum of the int values in an array:

public class PIArrays
{
    public int sum(final int[] array) {
        int sum = 0;
        for(int i = 0; i < array.length; i++){
            sum += array[i];
        }
        return sum; 
    }

}

Then we are supposed to write a test with the help of BlueJ's tools.

public void testSum()
{
    PIArrays pIArrays1 = new PIArrays();
    assertEquals(3, pIArrays1.sum({1,2}));
}   

The sum method works just fine, however, the test results in this error message:

"illegal start of expression"

What exactly am I doing wrong?

EDIT:

public class PIArraysTest is wrong it's actually public class PIArrays. I copied the wrong line.

The error happens in my testing class: public class PIArraysTest in this line:

assertEquals(3, pIArrays1.sum({1,2}));

with this part:

({1,2})

beeing highlighted

Upvotes: 1

Views: 537

Answers (1)

H&#233;ctor Valls
H&#233;ctor Valls

Reputation: 26084

{1, 2} as int[] is not a valid parameter. Use new int[]{1, 2} instead.

assertEquals(3, pIArrays1.sum(new int[]{1,2}));

Upvotes: 3

Related Questions