用java实现J-unit

用java实现J-unit,java,unit-testing,testing,Java,Unit Testing,Testing,我试图为下面的每个函数创建j单元测试,但我不知道如何开始。在我的例子中,很难使用函数本身并在单元用例中测试它。我只知道如何测试特定的值/列表。我需要帮助理解如何测试函数本身 public class FindValues { /** * @param args */ public static void main(String[] args) { int[] x = new int[] {2,5,6};

我试图为下面的每个函数创建j单元测试,但我不知道如何开始。在我的例子中,很难使用函数本身并在单元用例中测试它。我只知道如何测试特定的值/列表。我需要帮助理解如何测试函数本身

public class FindValues {           
    /**
     * @param args
     */
    public static void main(String[] args) {        
        int[] x = new int[] {2,5,6};
        int y = 2;
        findLast(x, y);
        int[] x1 = new int[] {1,2,0};
        lastZero(x1,y);
        int[] x2  = new int[] {-3, 3, 5, 0};
        countPositive(x2,y);
        int[] x3  = new int[] {-6, 2, -1, 1};
        oddOrPos(x3);
    }   

    // findlast(int [] x, int y) takes an array of integers and should return the index of
    // the last element in the array that is equal to y.
    public static int findLast(int [] x, int y) {
        x = new int[]{2,5,6};
        y = 2;
        for (int i=x.length-1; i >= 0; i--) { 
            if (x[i] == y) { 
                // System.out.println(i);
                return i;
            } 
        }
        return -1;          
    } //end FindLast

// lastZero(int [] x) takes an array of integers and should return the index of the last//0 in x.
    public static int lastZero (int[] x, int y) {
        x = new int[]{1,2,0};
        for (int i = 0; i < x.length; i++)
        {
            if (x[i] == 0)
            {
                System.out.println(i);
            }
        }
        return -1;
    }

    public static int countPositive (int[] x, int y) {
        //Returns the number of positive elements in Xint count = 0;
        x = new int[]{-3, 3, 5, 0};
        int count = 0;
        for (int i=0; i < x.length; i++) {
            if (x[i] > 0) {
                count++;
            }
        }
        System.out.println(count);
        return count;           
    }

    public static int oddOrPos(int[] x) {
        //Return numbers in x that are either odd or positive or both int count = 0;
        int count = 0;
        x = new int[]{-6, 2, -1, 1};
        for (int i = 0; i < x.length; i++) {
            if (x[i]% 2 == -1 || x[i]% 2 == 1 || x[i] > 0) {
                count++;
            }
        }
        System.out.println(count);
        return count;
        }
    }
公共类findValue{
/**
*@param args
*/
公共静态void main(字符串[]args){
int[]x=新的int[]{2,5,6};
int y=2;
findLast(x,y);
int[]x1=新的int[]{1,2,0};
lastZero(x1,y);
int[]x2=新的int[]{-3,3,5,0};
阳性(x2,y);
int[]x3=新的int[]{-6,2,-1,1};
oddOrPos(x3);
}   
//findlast(int[]x,int y)接受一个整数数组,并应返回
//数组中等于y的最后一个元素。
公共静态int findLast(int[]x,int y){
x=新的int[]{2,5,6};
y=2;
对于(inti=x.length-1;i>=0;i--){
如果(x[i]==y){
//系统输出打印LN(i);
返回i;
} 
}
返回-1;
}//结束FindLast
//lastZero(int[]x)接受一个整数数组,并应返回x中最后//0的索引。
公共静态int lastZero(int[]x,int y){
x=新的int[]{1,2,0};
对于(int i=0;i0){
计数++;
}
}
系统输出打印项次(计数);
返回计数;
}
公共静态int-oddOrPos(int[]x){
//返回x中的奇数或正数,或两者的int count=0;
整数计数=0;
x=新的int[]{-6,2,-1,1};
对于(int i=0;i0){
计数++;
}
}
系统输出打印项次(计数);
返回计数;
}
}

您应该从阅读和查找示例和教程开始

基本上,您要做的是创建一个JUnit类来测试
findValue
类。通常,您会以正在测试的类命名该类。例如,
FindValuesTest

我将让您从一个包含基本断言的非常基本的测试开始,但我认为您值得阅读文档并使用JUnit一段时间。一旦你在创建JUnit时遇到困难,那么你应该回到这里,通过添加你的尝试和你遇到的问题的所有相关细节来编辑你的问题。(见附件)

正如承诺的那样,这里有一个非常基本的示例可以帮助您开始:

import static org.junit.Assert.*;

import org.junit.Test;

public class FindValuesTest {

    @Test
    public void test() {
        int actualResult = FindValues.findLast(new int[]{1,2,3,4,2,5,8,2,9}, 2);
        assertEquals(7, actualResult);
    }

}
请注意,我删除了
findLast
中的硬编码参数值:

public static int findLast(int[] x, int y) {
    for (int i = x.length - 1; i >= 0; i--) {
        if (x[i] == y) {
            // System.out.println(i);
            return i;
        }
    }
    return -1;
} 
看看测试是如何向方法提供输入并断言输出符合预期的——这就是JUnit要做的。执行一些要测试的代码,并根据测试提供的输入验证代码的行为