JUnit:我缺少什么

时间:2018-09-30 04:32:56

标签: java recursion junit tostring

有人可以告诉我JUnit如何运行以及如何修复此代码吗?我确定我找不到一堆错误。

方法代码:

   public static String findMinimumRecursive(String[] array, String 
currentMinString){// another bonus method
        if(array.length == 0)
            return currentMinString;
            return findMinimumRecursive(Arrays.copyOfRange(array, 1, 
array.length), 
((Integer)currentMinString.length()).compareTo(array[0].length()) < 0 ?  
currentMinString : array[0]); 

  }

toString代码:

public String toString()
{
    String result;
   result = " result : " + (findMinimumRecursive(stringArray));
   return recursiveCompare;
}

JUnit代码:

import org.junit.*;
import static org.junit.Assert.*;

public class recursiveCompareTester{

    private String [] tringArray = new String[4];

    @Before
    public void setUp() {
        stringArray[4] = {"delta","alpha","omega","zeta"}
    }

    @Test
    public void recursiveCompareTester() {
        assertEquals(findMinimumRecursive(stringArray).toString,zeta );
    }

}

1 个答案:

答案 0 :(得分:0)

为什么在这里使用toString方法,这是不必要的。首先删除toString方法。然后要测试的类应如下所示。

public class RecursiveCompare {
    public static String findMinimumRecursive(String[] array, String currentMinString) {// another bonus method
        if (array.length == 0)
            return currentMinString;
        return findMinimumRecursive(Arrays.copyOfRange(array, 1, array.length),
                ((Integer) currentMinString.length()).compareTo(array[0].length()) < 0 ? currentMinString : array[0]);

    }
}

我在测试类中声明数组时遇到了一些问题,并且我也已修复它们。这是测试类的正确版本。

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class RecursiveCompareTester {
    private String[] stringArray;

    @Before
    public void setUp() {
        stringArray = new String[] { "delta", "alpha", "omega", "zeta" };
    }

    @Test
    public void recursiveCompareTester() {
        assertEquals(RecursiveCompare.findMinimumRecursive(stringArray, "invalid"), "zeta");
    }
}

该测试只是通过。