簡體   English   中英

Junit對於UCB cs61B使用IntelliJ沒有顯示任何結果(org.junit.Assert.assertArrayEquals())

[英]Junit did not show any results using IntelliJ for UCB cs61B (org.junit.Assert.assertArrayEquals() )

我正在獨自學習CS61B-UCB,並且是使用IntelliJ和Junit4.12的初學者。 我發現my org.junit.Assert.assertArrayEquals()沒有結果

在視頻中有這樣的節目

在運行窗口中。

這是TestSort.java的代碼

import static org.junit.Assert.*;
import org.junit.Test;
/** Tests the the Sort class. */
public class TestSort {
/** Test the Sort.sort method. */
@Test
public void testSort() {
    String[] input = {"i", "have", "an", "egg"};
    String[] expected = {"an", "egg", "have", "i"};

    Sort.sort(input);
    if (input != expected)
    {
        System.out.println("something wrong!");
    }

    org.junit.Assert.assertArrayEquals(expected, input);
}
@Test
public void testFindSmallest() {
    String[] input = {"i", "have", "an", "egg"};
    int expected = 2;

    int actual = Sort.findSmallest(input, 0);
    assertEquals(expected, actual);

    String[] input2 = {"there", "are", "many", "pigs"};
    int expected2 = 2;

    int actual2 = Sort.findSmallest(input2, 2);
    assertEquals(expected2, actual2);
}

@Test
public void testSwap() {
    String[] input = {"i", "have", "an", "egg"};
    int a = 0;
    int b = 2;
    String[] expected = {"an", "have", "i", "egg"};

    Sort.swap(input, a, b);
    assertArrayEquals(expected, input);
}
}

這是Sort.java的代碼

public class Sort {
public static void sort(String[] x) {
    sort(x, 0);
}

private static void sort(String[] x, int start) {
    if (start == x.length) {
        return;
    }
    int smallestIndex = findSmallest(x, start);
    swap(x, start, smallestIndex);
    sort(x, start + 1);
}

public static void swap(String[] x, int a, int b) {
    String temp = x[a];
    x[a] = x[b];
    x[b] = temp;
}

public static int findSmallest(String[] x, int start) {
    int smallestIndex = start;
    for (int i = start; i < x.length; i += 1) {
        int cmp = x[i].compareTo(x[smallestIndex]);

        if (cmp < 0) {
            smallestIndex = i;
        }
    }
    return smallestIndex;
}
}

我認為Junit的功能是獲取綠色部分,該部分顯示我的代碼如何工作,並獲取兩個字符串是否相等的結果。

關於IntelliJ的另一個問題是,我運行它與使用終端進行編譯和操作之間是否有區別? 因為當我使用終端時,它將顯示如下內容

在此處輸入圖片說明

我對此進行了很多搜索,它總是說我沒有將Junit.jar應用於classpath。 我檢查了是否添加了圖書館。 在此處輸入圖片說明

僅供參考,您可以在此處獲取庫在此處輸入鏈接說明

我調試了testSort函數,它在輸入部分和sort函數部分都運行良好。 雖然它給了我提示在此處輸入圖像描述的提示,但我選擇了下載,它顯示了未找到的源在此處輸入圖像描述 ,當我從現有文件中選擇源時在此處輸入圖像描述 ,它一直在附加....如何解決這個問題?

您的代碼可能未按預期運行,但它的運行完全像經驗豐富的Java開發人員所期望的那樣。 讓我解釋...

您已經發現=運算符的行為(或更准確地說,在本例中為!= )經常使經驗不足的Java工程師感到厭煩。 =運算符不知道如何使用數組,因此它只能用於比較引用。 在您的情況下,它正在比較inputexpected查看它們是否引用了完全相同的對象。 在您的代碼中, inputexpected都被聲明為新數組,因此是不同的單獨對象; 他們沒有引用相同的對象。

至於assertArrayEquals可能根本不使用=運算符。 雖然我沒有查看該方法的源代碼,但我敢冒險猜測它首先檢查引用是否相等(它們是否都引用同一個對象,然后檢查它們是否均為數組,然后檢查是否每個數組都expected要素也在input

數組會增加平等的困惑,因為存在許多平等的定義。 平等可以定義為...

  • 兩個數組具有相同數量的相同順序的元素
  • 這兩個數組具有相同數量的元素,但順序不同
  • 一個數組有5個元素,而另一個數組有10個元素,其中第一個數組的所有5個元素也在第二個數組中
  • 等等

我的一個建議可能會幫助您更好地理解這個問題(以及將來可能會遇到的更多問題),請看一下無法正常工作的方法的源代碼, assertArrayEquals在這個案例。 IntelliJ允許您導航到源代碼,或者如果源代碼不可用,請查看反編譯的字節碼。 在Mac上,只需在命令上單擊鼠標右鍵。 在Windows中,可以按住Control鍵並單擊。 (對不起,IntelliJ有太多不同的快捷方式設置,我無法具體說明。)

有關此主題的更多信息... Java中== vs equals()之間有什么區別? https://javabeginnerstutorial.com/core-java-tutorial/java-equals-method-vs-operator/

暫無
暫無

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

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