首页 文章

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

提问于
浏览
-1

Assignment: 编写一个JUnit测试,假设您有一个int值数组,并且您只希望JUnit测试在任何值小于20时失败 .

我知道它只是要求JUnit假设已经创建了其他方法 . 但无论如何我想创造它们 . 但是我不知道如何去做 . 到目前为止这是我的代码:

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;
            }

        }


    }

}

对于arrayLessThan Eclipse告诉我我需要返回一个布尔值,但是我不知道如何在没有for循环的情况下迭代数组 . 如果我在for循环之外返回true或false,它将失败我尝试用if / else语句做的目的 . 我该怎么做?对你的帮助表示感谢 .

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);
    }

}

3 回答

  • 0

    Eclipse(实际上是java编译器)抱怨,因为在 for 循环之后,您的方法不会返回 boolean . 编译器没有发现该方法永远不会那么远,因为它将始终在其第一次迭代期间返回 . 无论如何,这是一个问题,因为你的方法永远不会超越第一个数组元素 .

    像这样编码循环的典型方法是这样的:

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

    但除此之外,你需要编写一个框架,并且需要将测试编写为测试类的方法,这些测试类是以该框架所需的非常特定的方式编写的 . 您没有编写自己的 main 函数 - 该框架提供了一个查看代码以查找所有测试类和每个类中实现的测试的函数,然后为您运行这些测试 .

    您应该谷歌搜索JUnit的文档/教程/示例,然后再试一次 .

  • 0

    这个问题似乎比"how do I write a JUnit test"更多关于"why does this not compile and what the method return for an empty set" . 我建议您阅读一些JUnit教程(如this tutorial from mkyong)以了解它们 . 我会尝试回答我认为的第一个问题 .

    首先要根据您的描述注意循环的正确性 . 循环当前将始终返回基于数组的第一个值的值:

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

    根据您的描述,如果 any 项目与您的predicate(项目小于20)匹配,则应仅返回 false . 您还遇到编译器错误,因为它不返回任何空数组(0个元素) . 这将是改变它的一种方法:

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

    如果我在for循环之外返回一个真或假,它将破坏我试图用if / else语句做的目的

    嗯,不是真的 . 这取决于你想如何建模0元素数组的情况 . 对其进行建模的最佳方法是返回 true ,因为您无法指向不满足条件的元素 . 这被称为vacuous truth .

    您可以使用 anyMatchallMatch 以及空白真相in this question/answer来阅读Java 8流的良好解释 .

  • 4

    我很困惑,有两个问题......

    首先,这不是一个junit测试 .

    它们看起来像这样:

    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.
        }
    }
    

    其次,是方法arrayLessThan

    让我们一步一步地完成它:

    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.
    }
    

    现在看一下,我们看到它没有编译,因为没有返回语句用于空数组的情况 . 我们也看到它只检查第一个元素!查看 continue 的作用,它将解决仅检查第一个元素或以不同方式编写条件的问题 .

相关问题