简体   繁体   English

JUnit测试方法失败

[英]JUnit testing method fails

I'm stuck on this simple JUnit testing method as I'm not sure how to pass this method that has iterator. 由于不确定如何传递具有迭代器的方法,因此我只能使用这种简单的JUnit测试方法。 If anyone can take a look that would be really helpful. 如果有人可以看看,那将非常有帮助。

Here is main class: 这是主类:

public class Main {
    public static void main(String[] args) {

    final List<Integer> numbers = Arrays.asList(3, 4, 6, 1, 9);
    final Integer x = Integer.valueOf(1);
    System.out.println(findSame(numbers.iterator(), x, 0));
}
public static final int findSame(Iterator<Integer> iterator, Integer x, int idx) {
    if (!iterator.hasNext()) {
        return -1;
    }

    if (iterator.next().equals(x)) {
        return idx;
    } else {
        return findSame(iterator, x, idx+1);
    }
}

} }

Here is my testing trial method, which is not functional. 这是我的测试试用方法,不起作用。 I'm having trouble with iterator mainly and how to pass these particular values in this testing method: 我主要在使用迭代器以及如何在此测试方法中传递这些特定值时遇到麻烦:

@Test
public void searchNumReturnsIndex1(){
    Main instance = new Main();        

    int x = 1;

    Iterator<Integer> iterator;
    int result = Main.findSame(null, x, 3);
    assertEquals(2, instance.findSame(null,x, 3));  
    }

Assuming you are doing this to practice passing an iterator as an argument, what you should do is this: 假设您这样做是为了练习将迭代器作为参数传递,那么您应该做的是:

public class main {
    public static void main(String[] args) {
        // put whatever you want here, but if 'findSome' gets it's iterator as an argument,
        // then you shouldn't be trying to define it's list here. It's list is being passed
        // to it as an argument.
    }

    /* don't put final here unless you are working with subclasses and don't want them making their own versions of findSame */
    public static  int findSame(Iterator<Integer> iterator, Integer x, int idx) {
        if(!iterator.hasNext()) {
            return -1;
        }

        if(iterator.next().equals(x)) {
            return idx;
        } else {
            return findSame(iterator, x, idx+1);
        }
    }
}

And the test should be something like this: 测试应该是这样的:

@Test
public void searchNumReturnsIndex1() {
    Main instance = new Main();

    List<Integer> numbers = Arrays.asList(3, 4, 6, 1, 9);

    Iterator<Integer> iterator = numbers.iterator();

    int result = instance.findSame(iterator, 1, 3);
    assertEquals(2,result);
}

This should compile and test what your code was trying to test. 这应该编译并测试您的代码要测试的内容。 There are some issues with your design though. 但是,您的设计存在一些问题。

First, you shouldn't have findSame take that third argument, unless for some reason you want the ability to only check part of the list. 首先,您不应该让findSame接受第三个参数,除非出于某种原因您只希望能够检查部分列表。 Something like what's below will be more efficient, and the function will be simpler. 像下面这样的东西会更高效,功能也会更简单。

findSame(Iterator<Integer> iterator, Integer x) {
    while(iterator.hasNext()) {
        if(iterator.next().equals(x)) {
            return iterator.previousIndex();
        }
    }
    return -1;
}

Instead of thinking "findSome checks if this iterator contains this element at the given index or beyond" you can think "findSome checks if this iterator contains this element". 可以不用考虑“ findSome检查此迭代器是否在给定索引处或超出此元素”,而可以考虑“ findSome检查此迭代器是否包含此元素”。

Second, you should always style your code consistently. 其次,您应该始终对代码进行样式设置。 You didn't indent your code consistently, findSame was indented the same amount as public class main , which made me think findSame was on the same level as the class main, not contained inside main. 您没有一致地缩进代码,findSame缩进了与public class main相同的数量,这使我认为findSame与class main处于同一级别,而不包含在main内部。

Indenting should tell you about the structure of the program at a glance, so you don't have to read every detail of a program to get the general flow of it. 缩进一眼就能告诉您程序的结构,因此您不必阅读程序的每个细节即可了解程序的一般流程。

如果我对它的理解很好...'searchNum'函数将在'数字'列表中返回'x'的索引...在这种情况下,'6'位于第三位置(index = 2),因此您需要比较的价值:

 assertEquals(2, instance.searchNum(x, 0));

Change from

 assertEquals(1, instance.searchNum(x, 2));  

to

 assertEquals(2, instance.searchNum(x, 2));  

and then see whether it works or not. 然后查看它是否有效。 It will surely work if 6 is at second index of numbers array as you have mentioned in code above. 如上面的代码中所述,如果6在数字数组的第二个索引处肯定会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM