簡體   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

分配:假設您有一個int值數組,並且僅希望任何一個小於20的JUnit測試失敗,請編寫一個JUnit測試。

我知道它只是在假設其他方法已經創建的情況下才要求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);
    }

}

Eclipse(實際上是Java編譯器)抱怨,因為在for循環之后,您的方法未返回boolean 編譯器不會弄清楚該方法永遠不會走得太遠,因為它總是在第一次迭代時返回。 無論如何,這都是一個問題,因為您的方法永遠不會超出第一個數組元素。

編寫這樣的循環的典型方法如下:

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

但是除此之外,您還缺少JUnit的要點。 這是一個框架,您需要將測試作為測試類的方法編寫,並以該框架要求的非常特定的方式編寫。 您不必編寫自己的main功能-框架提供了一個功能,該功能可通過代碼查找所有測試類以及在每個類中實現的測試,然后為您運行這些測試。

您應該在Google上搜索JUnit的文檔/教程/示例,然后重試。

這個問題似乎更多地是關於“為什么不編譯以及對於空集該方法返回什么”而不是“如何編寫JUnit測試” 我建議閱讀一些JUnit教程(例如mkyong的本教程 )以了解它們。 我將嘗試回答我認為的第一個問題。

首先要根據您的描述注意循環的正確性。 循環當前將始終基於數組的第一個值返回一個值:

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

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

    }
}

根據您的描述,僅當任何一項與您的謂詞匹配(一項小於20)時,它才應返回false 您還會遇到編譯器錯誤,因為它不會為空數組(0個元素)返回任何內容。 這將是更改它的一種方法:

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

而且,如果我在for循環外返回true或false,它將無法達到我嘗試使用if / else語句的目的

好吧,不是真的。 這取決於您要如何建模0元素數組的情況。 建模的最佳方法是返回true因為您不能指向不滿足您條件的元素。 這就是所謂的虛無事實

在此問題/答案中,您可以使用anyMatchallMatch和空虛事實閱讀有關Java 8流的很好的解釋。

我很困惑,有兩個問題...

首先是這不是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執行的操作,它將解決僅檢查第一個元素的問題,或者以其他方式編寫您的條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM