首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有java的J-unit

带有java的J-unit
EN

Stack Overflow用户
提问于 2018-09-25 07:06:29
回答 1查看 175关注 0票数 -1

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

代码语言:javascript
复制
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;
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2018-09-25 11:10:36

您应该从阅读JUnit User Guide和执行research开始查找示例和教程。

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

我将从一个包含基本断言的非常基本的测试开始,但我认为值得您去阅读文档并尝试一下JUnit。一旦你在创建JUnit时遇到了made an attempt问题,你应该回到这里,通过添加你的尝试和所有相关的问题细节来编辑你的问题。(参见How to Ask a Good Question)

正如承诺的那样,这里有一个非常基本的示例来帮助您入门:

代码语言:javascript
复制
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中的硬编码参数值:

代码语言:javascript
复制
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要做的事情。执行一些要测试的代码,并根据测试提供的输入验证代码的行为。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52488471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档