J-unit with java

问题描述 投票:-1回答:1

我正在尝试为下面的每个函数创建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;
        }
    }
java unit-testing testing
1个回答
0
投票

您应该首先阅读JUnit User Guide并使用research查找示例和教程。

基本上你想要做的是创建一个JUnit类来测试你的FindValues类。通常,您会在正在测试的类之后命名此类。例如,FindValuesTest

我将帮助您开始一个包含基本断言的基本测试,但我认为您有必要阅读文档并使用JUnit一段时间。一旦你有made an attempt创建JUnit并陷入困境,那么你应该回到这里,通过添加你的尝试和你所遇问题的所有相关细节来编辑你的问题。 (见How to Ask a Good Question

正如所承诺的那样,这是一个非常基本的示例,可以帮助您入门:

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的意图。您执行一些要测试的代码,并根据测试提供的输入验证该代码的行为。

© www.soinside.com 2019 - 2024. All rights reserved.