Hi,
Im new here and to java programming in general. I've been given an assignment to write a sorting algorithm and then test it using JUnit tests. The problem is when I try to compile the tester class i get a ".class expected" errors followed by three "; expected" errors for each test method I've written. Any help greatly appreciated!
Here's my tester code:

import static org.junit.Assert.*;
import org.junit.Test;
 
public class SorterTester{
    
    // Create new sorter object.
    Sorter sort = new Sorter();
              
    @Test(timeout=15)
    public void boundaryTest(){
        int[] boundaryInts = { 0, 1, -9999, -1, 9999, 10001, 10000, -10000 };
        int[] correctBoundarySort = { -10000, -9999, -1, 0, 1, 9999, 10000, 10001 };
        assertArrayEquals(int[] correctBoundarySort, int[] sort.sort(boundaryInts));
    }   
    
    /*
     * Test whether the method can sort correctly when multiple numbers are the 
     * same value
     */
    @Test(timeout=15)
    public void duplicateSort(){
        int[] duplicateInts = { 1, 1, -1, -1, -69, - 69, 0, 0, 0, 1000, 1000 };
        int[] correctDuplicateSort = { -69, -69, -1, -1, 0, 0, 0, 1, 1, 1000, 1000};
        assertArrayEquals(int[] correctDuplicateSort, int[] sort.sort(duplicateInts));
    }
    
    /*
     * Tests does the sorter work when an array of only one digit is passed.
     */
    @Test(timeout=15)
    public void singleSort(){
        int[] singleInt = { 8 };
        //Duplicate of singleInt needed because singleInt is always = singleInt
        int[] correctSingleSort = { 8 };
        assertArrayEquals( int[] correctSingleSort, int[] sort.sort( singleInt ));
    }
        
}

Here's my sorter code:

public class Sorter{
    public void sort( int[] arrayToSort ){
        for( int index = 0; index < arrayToSort.length; index++ ){
            for( int position = index; position < arrayToSort.length; 
            position++ ){
                if( arrayToSort[ position ] < arrayToSort[ index ] ){
                    int temp = arrayToSort[ index ];
                    arrayToSort[ index ] = arrayToSort[ position ];
                    arrayToSort[ position ] = temp;
                }    
            }         
        }
    }
}

And here's the error I get:

javac Sorter.java SorterTester.java 
SorterTester.java:19: '.class' expected
        assertArrayEquals(int[] correctBoundarySort, int[] sort.sort(boundaryInts));
                                ^
SorterTester.java:19: ';' expected
        assertArrayEquals(int[] correctBoundarySort, int[] sort.sort(boundaryInts));
                                                   ^
SorterTester.java:19: ';' expected
        assertArrayEquals(int[] correctBoundarySort, int[] sort.sort(boundaryInts));
                                                               ^
SorterTester.java:19: ';' expected
        assertArrayEquals(int[] correctBoundarySort, int[] sort.sort(boundaryInts));
                                                                                  ^
SorterTester.java:30: '.class' expected
        assertArrayEquals(int[] correctDuplicateSort, int[] sort.sort(duplicateInts));
                                ^
SorterTester.java:30: ';' expected
        assertArrayEquals(int[] correctDuplicateSort, int[] sort.sort(duplicateInts));
                                                    ^
SorterTester.java:30: ';' expected
        assertArrayEquals(int[] correctDuplicateSort, int[] sort.sort(duplicateInts));
                                                                ^
SorterTester.java:30: ';' expected
        assertArrayEquals(int[] correctDuplicateSort, int[] sort.sort(duplicateInts));
                                                                                    ^
SorterTester.java:41: '.class' expected
        assertArrayEquals( int[] correctSingleSort, int[] sort.sort( singleInt ));
                                 ^
SorterTester.java:41: ';' expected
        assertArrayEquals( int[] correctSingleSort, int[] sort.sort( singleInt ));
                                                  ^
SorterTester.java:41: ';' expected
        assertArrayEquals( int[] correctSingleSort, int[] sort.sort( singleInt ));
                                                              ^
SorterTester.java:41: ';' expected
        assertArrayEquals( int[] correctSingleSort, int[] sort.sort( singleInt ));
                                                                                ^
12 errors

As I said I'm a beginner so apologies if this is really obvious but it's been wrecking my head for 2 hours now:'(

assertArrayEquals(int[] correctDuplicateSort, int[] sort.sort(duplicateInts));

assertArrayEquals expects two array references as its parameters; what you've got there is an array declaration. The correct way would be:

assertArrayEquals(correctDuplicateSort, sort.sort(duplicateInts));

Thanks very much, that solved it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.