简体   繁体   English

为数组中的数字小于20的数组创建JUnit,尝试创建假定的数组,然后进行测试

[英]Creating a JUnit for an array with a number less than 20 in the array, trying to create the assumed array, to then test

Assignment: Write a JUnit test assuming you have an array of int values and you only want the JUnit test to fail if any of the values are less than 20. 分配:假设您有一个int值数组,并且仅希望任何一个小于20的JUnit测试失败,请编写一个JUnit测试。

I know it only asks for the JUnit assuming the other methods are already created. 我知道它只是在假设其他方法已经创建的情况下才要求JUnit。 But I want to create them anyway. 但是我还是想创建它们。 However I do not know how to go about it. 但是我不知道该怎么做。 This is my code so far: 到目前为止,这是我的代码:

package ArrayJU;

public class ArrayJUTest {

    public static void main(String[] args){
        ArrayJUTest array = new ArrayJUTest();
        int arr[] = {23,25,50,68,3};
        System.out.println(array.arrayLessThan(arr));
    }

    public boolean arrayLessThan(int array[]){
        for (int element : array) {

            if(element>20){
                return true;
            }
            else{
                return false;
            }

        }


    }

}

For the arrayLessThan Eclipse is telling me that I need to return a boolean, however I wouldn't know how to iterate through the array without a for loop. 对于arrayLessThan,Eclipse告诉我我需要返回一个布尔值,但是我不知道如何在没有for循环的情况下遍历数组。 And if I return a true or a false outside the for loop it will defeat the purpose of what I'm trying to do with the if/else statements. 而且,如果我在for循环之外返回true或false,它将无法达到我尝试使用if / else语句的目的。 How do I go about this? 我该怎么办? Your help will be greatly appreciated. 对你的帮助表示感谢。

JUnit Test: JUnit测试:

package ArrayJU;

import static org.junit.Assert.*;

import org.junit.Test;

public class JUnitArrayTest {

    @Test
    public void JUnitArTest(){
        int[] arr = {32,52,89,12};
        ArrayJUTest arra = new ArrayJUTest();
        boolean poop = arra.arrayLessThan(arr);
        assertEquals(false, poop);
    }

}

Eclipse (really the java compiler) is complaining because after your for loop, your method doesn't return a boolean . Eclipse(实际上是Java编译器)抱怨,因为在for循环之后,您的方法未返回boolean The compiler doesn't figure out that the method never gets that far because it will always return during its very first iteration. 编译器不会弄清楚该方法永远不会走得太远,因为它总是在第一次迭代时返回。 Which is a problem anyway, since your method will never look beyond the first array element. 无论如何,这都是一个问题,因为您的方法永远不会超出第一个数组元素。

The typical way to code a loop like this is something along these lines: 编写这样的循环的典型方法如下:

public boolean arrayLessThan(int[] array) {
  for (int element: array) {
    if (element < 20) {
      return false;
    }
  }
  return true;
}

But beyond this, you're missing the point of JUnit. 但是除此之外,您还缺少JUnit的要点。 It's a framework, and you need to write your tests as methods of test classes that are written in a very specific manner required by that framework. 这是一个框架,您需要将测试作为测试类的方法编写,并以该框架要求的非常特定的方式编写。 You don't write your own main function - the framework provides one that looks through your code to find all your test classes and the tests implemented in each class, and then runs those tests for you. 您不必编写自己的main功能-框架提供了一个功能,该功能可通过代码查找所有测试类以及在每个类中实现的测试,然后为您运行这些测试。

You should google for documents / tutorials / examples of JUnit and then try again. 您应该在Google上搜索JUnit的文档/教程/示例,然后重试。

This question seems to be more about "why does this not compile and what the method return for an empty set" than "how do I write a JUnit test" . 这个问题似乎更多地是关于“为什么不编译以及对于空集该方法返回什么”而不是“如何编写JUnit测试” I would recommend reading up on some JUnit tutorials (like this tutorial from mkyong ) to get an understanding of them. 我建议阅读一些JUnit教程(例如mkyong的本教程 )以了解它们。 I'll try to answer what I think is the first question. 我将尝试回答我认为的第一个问题。

The first thing is to note about the correctness of your loop based on your description. 首先要根据您的描述注意循环的正确性。 The loop will currently always return a value based on the first value of the array: 循环当前将始终基于数组的第一个值返回一个值:

public boolean arrayLessThan(int array[]){
    for (int element : array) {

        if(element>20){
            return true;
        }
        else{
            return false;
        }

    }
}

Based on your description, it should only return false if any item matches your predicate (an item is less than 20). 根据您的描述,仅当任何一项与您的谓词匹配(一项小于20)时,它才应返回false You are also getting a compiler error because it does not return anything for an empty array (0 elements). 您还会遇到编译器错误,因为它不会为空数组(0个元素)返回任何内容。 This would be one way to change it: 这将是更改它的一种方法:

public boolean arrayLessThan(int array[]){
    for (int element : array) {
        if(element < 20){
            return false;
        }
    }
    return true;
}

And if I return a true or a false outside the for loop it will defeat the purpose of what I'm trying to do with the if/else statements 而且,如果我在for循环外返回true或false,它将无法达到我尝试使用if / else语句的目的

Well, not really. 好吧,不是真的。 It depends on how you want to model the 0-element array case. 这取决于您要如何建模0元素数组的情况。 The best way to model it would be to return true because you cannot point to an element that does not satisfy your condition. 建模的最佳方法是返回true因为您不能指向不满足您条件的元素。 This is known as vacuous truth . 这就是所谓的虚无事实

You can read a good explanation for Java 8 streams with anyMatch and allMatch and vacuous truth in this question/answer . 在此问题/答案中,您可以使用anyMatchallMatch和空虚事实阅读有关Java 8流的很好的解释。

I'm confused, there are two issues... 我很困惑,有两个问题...

The first is that this isn't a junit test. 首先是这不是junit测试。

They look like this: 他们看起来像这样:

import static org.junit.Assert.*;

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

class HypotheticalClassTest {

    private HypotheticalClass hypClass;

    @Before
    public void setup() {
        hypClass = new HypotheticalClass();
    }

    @Test
    public void ensureNoNumsLessThanTwenty() {
        int[] result = hypClass.hypotheticalMethod();
        // Some assertions. Check out the org.junit.Assert class.
    }
}

Second, is you method arrayLessThan 其次,你是方法arrayLessThan

Let's go through it step by step: 让我们逐步进行一下:

public boolean arrayLessThan(int array[]){
    for (int element : array) { // For each int in array...
        if(element>20){         // If element is greater than 20
            return true;        // Return true (returning stops the loop,
                                //              do you want to stop on the
                                //              first element greater than
                                //              20?)
        }                       //
        else{                   // otherwise
            return false;       // Return false
        }
    }                           // Assuming there are no elements in the
                                //   array (length 0) then this is where it
                                //   comes. There's no return here, there
                                //   needs to be, then it will stop
                                //   complaining.
}

Looking at it now we see it doesn't compile because there is no return statement for the case of an empty array. 现在来看它,因为没有空数组的情况,没有返回语句,因此它无法编译。 Also we see it only checks the first element! 我们也看到它只检查第一个元素! Look up what continue does, it will fix the issue of only checking the first element, or write your condition differently. 查找continue执行的操作,它将解决仅检查第一个元素的问题,或者以其他方式编写您的条件。

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

相关问题 我正在尝试创建一个错误消息,如果在数组中输入的数据小于零,输入验证 - I'm trying to create an error message if the data entered in an array is less than zero, input validation 查找数组在小于O(n ^ 2)内重复的次数 - Find the number of times a number is repeated in an array in less than O(n^2) JUnit测试创建相同的数组而不是随机数组 - JUnit test creating the same array instead of random one For循环创建的数组的值比预期的少 - For loop creating an array with one less value than expected 用于在数组中查找最大数量的参数化Junit测试用例的期望值 - Expected values for Parameterized Junit test case for finding Max number in an array 数组元素与平均值不同,小于数字 - Elements of array differ from average and less than number 如何使用JUnit测试对象数组 - How to test an array of objects with JUnit Java数组长度小于0? - Java array length less than 0? 二维数组中的第一个用户输入(数字)应小于或等于二维数组中的其他元素 - The first user input(number) in the 2d array should be less than or equal to other element in the 2d array 我正在尝试使用一种方法来查找数组中的哪个数字大于20并返回一个百分比 - I am trying to use a method in order to find which numbers in the array are greater than 20 and return a percentage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM