国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 幫助中心 > 正文

Spring Boot 單元測試和集成測試實現(xiàn)詳解

2024-07-09 22:40:44
字體:
供稿:網(wǎng)友

學(xué)習(xí)如何使用本教程中提供的工具,并在 Spring Boot 環(huán)境中編寫單元測試和集成測試。

1. 概覽

本文中,我們將了解如何編寫單元測試并將其集成在 Spring Boot 環(huán)境中。你可在網(wǎng)上找到大量關(guān)于這個主題的教程,但很難在一個頁面中找到你需要的所有信息。我經(jīng)常注意到初級開發(fā)人員混淆了單元測試和集成測試的概念,特別是在談到 Spring 生態(tài)系統(tǒng)時。我將嘗試講清楚不同注解在不同上下文中的用法。

2. 單元測試 vs. 集成測試

維基百科是這么說單元測試的:

在計算機(jī)編程中,單元測試是一種軟件測試方法,用以測試源代碼的單個單元、一個或多個計算機(jī)程序模塊的集合以及相關(guān)的控制數(shù)據(jù)、使用過程和操作過程,以確定它們是否適合使用。

集成測試:

“集成測試(有時也稱集成和測試,縮寫為 I&T)是軟件測試的一個階段,在這個階段中,各個軟件模塊被組合在一起來進(jìn)行測試。”

簡而言之,當(dāng)我們在做單元測試時,只是測試了一個代碼單元,每次只測試一個方法,不包括與正測試組件相交互的其他所有組件。

另一方面,在集成測試中,我們測試各組件之間的集成。由于單元測試,我們可知這些組件行為與所需一致,但不清楚它們是如何在一起工作的。這就是集成測試的職責(zé)。

3. Java 單元測試

所有 Java 開發(fā)者都知道 JUnit 是執(zhí)行單元測試的主要框架。它提供了許多注解來對期望進(jìn)行斷言。

Hamcrest 是一個用于軟件測試的附加框架。Hamcrest 允許使用現(xiàn)有的 matcher 類來檢查代碼中的條件,還允許自定義 matcher 實現(xiàn)。要在 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); }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 东方市| 吕梁市| 庆安县| 泸定县| 健康| 三亚市| 莱阳市| 珲春市| 吉安市| 会东县| 青田县| 固安县| 万州区| 莲花县| 江安县| 彭山县| 桃园县| 衡东县| 东乡| 西乌珠穆沁旗| 田东县| 台北市| 乌审旗| 闽侯县| 广水市| 忻城县| 东方市| 射阳县| 吴忠市| 团风县| 徐州市| 读书| 宁津县| 方山县| 永兴县| 合水县| 铁岭市| 三都| 承德县| 兴城市| 拉孜县|