SetUp,初始化Junit测试

Mar*_*sen 7 java testing junit unit-testing

我正在尝试测试我的3个类,它们以不同的方式对字符串数组进行排序!

我知道有一种方法可以初始化一个数组,然后在我的每个测试中使用它们.

到目前为止这是我的代码:

public class SortingTest {

    public insertionSort is = new insertionSort();
    public bubbleSort bs = new bubbleSort();
    @Test
    public void testBubbleSort() {
        String [] sortedArray ={"Chesstitans", "Ludo", "Monkey", "Palle"};
        bs.sort(sortedArray);
        assertArrayEquals(sortedArray, x);
    }
    @Test
    public void testInsertionSort() {


    }
    @Test
    public void testMergeSort() {


    }
    @Test
    public void testSelectionSort() {


    }
    @Before
    protected void setUp() throws Exception{
        String[]x ={"Ludo", "Chesstitans", "Palle", "Monkey"};
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然我已经尝试了setUp和initialize方法但它似乎没有找到x我做错了什么?

mun*_*ngm 12

您需要创建类x的成员变量SortingTest

public class SortingTest {  

    private String[] x; 

    @Before
    public void init() {
      x = new String {"Ludo", "Chesstitans", "Palle", "Monkey"};
    }
}
Run Code Online (Sandbox Code Playgroud)