[英]Using assertArrayEquals in unit tests
我的意圖是使用API 中描述的assertArrayEquals(int[], int[])
JUnit 方法來驗證我的類中的一種方法。
但是 Eclipse 向我顯示了它無法識別這種方法的錯誤消息。 這兩個進口已經到位:
import java.util.Arrays;
import junit.framework.TestCase;
我錯過了什么?
這適用於JUnit 5 :
import static org.junit.jupiter.api.Assertions.*;
assertArrayEquals(new int[]{1,2,3},new int[]{1,2,3});
這應該適用於JUnit 4 :
import static org.junit.Assert.*;
import org.junit.Test;
public class JUnitTest {
/** Have JUnit run this test() method. */
@Test
public void test() throws Exception {
assertArrayEquals(new int[]{1,2,3},new int[]{1,2,3});
}
}
這對於舊的 JUnit 框架 ( JUnit 3 ) 來說是一樣的:
import junit.framework.TestCase;
public class JUnitTest extends TestCase {
public void test() {
assertArrayEquals(new int[]{1,2,3},new int[]{1,2,3});
}
}
注意區別:沒有注解,測試類是 TestCase 的子類(它實現了靜態斷言方法)。
如果您只想使用 assertEquals 而不依賴於您的 Junit 版本,這可能很有用
assertTrue(Arrays.equals(expected, actual));
嘗試添加:
import static org.junit.Assert.*;
assertArrayEquals
是一個靜態方法。
如果您正在編寫擴展TestCase 的JUnit 3.x 樣式測試,那么您不需要使用Assert
限定符 - TestCase 擴展 Assert 本身,因此這些方法無需限定符即可使用。
如果您使用 JUnit 4 注釋,避免使用 TestCase 基類,則需要Assert
限定符,以及導入org.junit.Assert
。 在這些情況下,您可以使用靜態導入來避免使用限定符,但某些人認為這些 樣式很差。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.