【问题标题】:How to actually sort a list in junit test in Eclipse?如何在 Eclipse 的 junit 测试中实际对列表进行排序?
【发布时间】:2019-10-13 09:25:50
【问题描述】:

我将如何设置此 JUnit 测试以实际正确排序?

我尝试了很多不同的方法来设置测试,但仍然无法正常工作。例如,尝试删除稍后添加的主要参数是行不通的。我已经尝试调整部件以使其工作,但它只是失败了。

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

无法对数组类型 int[] 调用 sort(int[]) 无法从计算机类型对非静态方法 sort(int[]) 进行静态引用

所以我不知道我应该如何设置这种类型才能正常工作。

【问题讨论】:

标签: java eclipse sorting arraylist junit


【解决方案1】:

我建议使用 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());    

        }    

}

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 2012-02-05
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多