學習如何使用本教程中提供的工具,并在 Spring Boot 環境中編寫單元測試和集成測試。
1. 概覽
本文中,我們將了解如何編寫單元測試并將其集成在 Spring Boot 環境中。你可在網上找到大量關于這個主題的教程,但很難在一個頁面中找到你需要的所有信息。我經常注意到初級開發人員混淆了單元測試和集成測試的概念,特別是在談到 Spring 生態系統時。我將嘗試講清楚不同注解在不同上下文中的用法。
2. 單元測試 vs. 集成測試
維基百科是這么說單元測試的:
在計算機編程中,單元測試是一種軟件測試方法,用以測試源代碼的單個單元、一個或多個計算機程序模塊的集合以及相關的控制數據、使用過程和操作過程,以確定它們是否適合使用。
集成測試:
“集成測試(有時也稱集成和測試,縮寫為 I&T)是軟件測試的一個階段,在這個階段中,各個軟件模塊被組合在一起來進行測試?!?/p>
簡而言之,當我們在做單元測試時,只是測試了一個代碼單元,每次只測試一個方法,不包括與正測試組件相交互的其他所有組件。
另一方面,在集成測試中,我們測試各組件之間的集成。由于單元測試,我們可知這些組件行為與所需一致,但不清楚它們是如何在一起工作的。這就是集成測試的職責。
3. Java 單元測試
所有 Java 開發者都知道 JUnit 是執行單元測試的主要框架。它提供了許多注解來對期望進行斷言。
Hamcrest 是一個用于軟件測試的附加框架。Hamcrest 允許使用現有的 matcher 類來檢查代碼中的條件,還允許自定義 matcher 實現。要在 JUnit 中使用 Hamcrest matcher,必須使用 assertThat
語句,后跟一個或多個 matcher。
在這里,你可以看到使用這兩種框架的簡單測試:
import static org.hamcrest.CoreMatchers.allOf;import static org.hamcrest.CoreMatchers.anyOf;import static org.hamcrest.CoreMatchers.both;import static org.hamcrest.CoreMatchers.containsString;import static org.hamcrest.CoreMatchers.equalTo;import static org.hamcrest.CoreMatchers.everyItem;import static org.hamcrest.CoreMatchers.hasItems;import static org.hamcrest.CoreMatchers.not;import static org.hamcrest.CoreMatchers.sameInstance;import static org.hamcrest.CoreMatchers.startsWith;import static org.junit.Assert.assertArrayEquals;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertFalse;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertNotSame;import static org.junit.Assert.assertNull;import static org.junit.Assert.assertSame;import static org.junit.Assert.assertThat;import static org.junit.Assert.assertTrue;import java.util.Arrays;import org.hamcrest.core.CombinableMatcher;import org.junit.Test;public class AssertTests { @Test public void testAssertArrayEquals() { byte[] expected = "trial".getBytes(); byte[] actual = "trial".getBytes(); assertArrayEquals("failure - byte arrays not same", expected, actual); } @Test public void testAssertEquals() { assertEquals("failure - strings are not equal", "text", "text"); } @Test public void testAssertFalse() { assertFalse("failure - should be false", false); } @Test public void testAssertNotNull() { assertNotNull("should not be null", new Object()); } @Test public void testAssertNotSame() { assertNotSame("should not be same Object", new Object(), new Object()); } @Test public void testAssertNull() { assertNull("should be null", null); } @Test public void testAssertSame() { Integer aNumber = Integer.valueOf(768); assertSame("should be same", aNumber, aNumber); } // JUnit Matchers assertThat @Test public void testAssertThatBothContainsString() { assertThat("albumen", both(containsString("a")).and(containsString("b"))); } @Test public void testAssertThatHasItems() { assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three")); } @Test public void testAssertThatEveryItemContainsString() { assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n"))); } // Core Hamcrest Matchers with assertThat @Test public void testAssertThatHamcrestCoreMatchers() { assertThat("good", allOf(equalTo("good"), startsWith("good"))); assertThat("good", not(allOf(equalTo("bad"), equalTo("good")))); assertThat("good", anyOf(equalTo("bad"), equalTo("good"))); assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4)))); assertThat(new Object(), not(sameInstance(new Object()))); } @Test public void testAssertTrue() { assertTrue("failure - should be true", true); }}
新聞熱點
疑難解答