-2

How would I go about setting this JUnit test to actually sort correctly?

I tried a bunch of different ways of setting up the test but it still isn't working. For example, trying to remove the main parameters to add later doesn't work. I've tried tweaking parts to make it work but it just fails.

       void test() {
        int [] input = new int[]{6,1,7,3,8,2,5,4};
        input.sort(input);
        assertEquals("1:2:3:4:5:6:7:8:",(input));
    }
         // Things I have tried
        //int[] result = new Computers().printArray(input);
        //input.sort(input);
        //assertEquals("1:2:3:4:5:6:7:8:",(input)); 
        //input.sort(input);
        //assertEquals(expected,numbers); 
        //int [] input = new int[]{6,1,7,3,8,2,5,4};
        //Computers sort = new Computers();
        //assertEquals("1:2:3:4:5:6:7:8:",(input));
        //int[] numbers = {6,1,7,3,8,2,5,4};
        //int[] expected = {1,2,3,4,5,6,7,8};   

}

        Here is my sort in Computers class

          public void sort(int arr[]) 
        { 
            int n = arr.length; 
            for (int i = 0; i < n-1; i++) 
                for (int j = 0; j < n-i-1; j++) 
                    if (arr[j] > arr[j+1]) 
                    { 
                        int temp = arr[j]; 
                        arr[j] = arr[j+1]; 
                        arr[j+1] = temp; 
                    } 
        } 
    static void printArray(int arr[]) { 
            int n = arr.length; 
            for (int i = 0; i < n; ++i) 
            System.out.print(arr[i] + " "); 
            System.out.println(); 
        } 

Cannot invoke sort(int[]) on the array type int[] Cannot make a static reference to the non-static method sort(int[]) from the type Computers

So I don't know how exactly I should set up this sort to work properly.

Neal. M
  • 5
  • 3
  • 1
    Possible duplicate of [Comparing arrays in JUnit assertions, concise built-in way?](https://stackoverflow.com/questions/4228161/comparing-arrays-in-junit-assertions-concise-built-in-way) – Bor Laze May 27 '19 at 20:45

1 Answers1

0

I suggest using Hamcrest:

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.assertThat;    

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;    

import org.hamcrest.collection.IsEmptyCollection;
import org.junit.Test;    

public class AssertLists {    

     @Test
        public void testAssertList() {    

            List<Integer> actual = Arrays.asList(1,2,3,4,5,6);
            List<Integer> expected = Arrays.asList(1,2,3,4,5,6);


            //1. Test equal.
            assertThat(actual, is(expected));    


            //2. Check List Size
            assertThat(actual, hasSize(6));    

            assertThat(actual.size(), is(6));    

            //3.  List order    

            // Ensure Correct order
            assertThat(expected, contains(1,2,3,4,5,6));    

            // Can be any order
            assertThat(expected, hasItems(3,2,6));    

            //4. check empty list
            assertThat(actual, not(IsEmptyCollection.empty()));    

            assertThat(new ArrayList<>(), IsEmptyCollection.empty());    

        }    

}
sirandy
  • 1,834
  • 5
  • 27
  • 32