溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JAVA中單元測(cè)試的常用方式(小結(jié))

發(fā)布時(shí)間:2020-09-16 14:56:04 來源:腳本之家 閱讀:187 作者:空無 欄目:編程語言

什么是單元測(cè)試

單元測(cè)試(英語:Unit Testing)又稱為模塊測(cè)試, 是針對(duì)程序模塊(軟件設(shè)計(jì)的最小單位)來進(jìn)行正確性檢驗(yàn)的測(cè)試工作。程序單元是應(yīng)用的最小可測(cè)試部件。在過程化編程中,一個(gè)單元就是單個(gè)程序、函數(shù)、過程等;對(duì)于面向?qū)ο缶幊蹋钚卧褪欠椒?,包括基類(超類)、抽象類、或者派生類(子類)中的方法?br />

通常來說,程序員每修改一次程序就會(huì)進(jìn)行最少一次單元測(cè)試,在編寫程序的過程中前后很可能要進(jìn)行多次單元測(cè)試,以證實(shí)程序達(dá)到軟件規(guī)格書要求的工作目標(biāo),沒有程序錯(cuò)誤;雖然單元測(cè)試不是什么必須的,但也不壞,這牽涉到項(xiàng)目管理的政策決定。

單元測(cè)試的優(yōu)點(diǎn)

優(yōu)質(zhì)的單元測(cè)試可以保障開發(fā)質(zhì)量和程序的魯棒性。在大多數(shù)互聯(lián)網(wǎng)企業(yè)中開發(fā)工程師在研發(fā)過程中都會(huì)頻繁地執(zhí)行測(cè)試用例,運(yùn)行失敗的單測(cè)能幫助我們快速排查和定位問題 使問題在被帶到線上之前完成修復(fù)。正如軟件工程界的一條金科玉律----越早發(fā)現(xiàn)的缺陷,其修復(fù)成本越低。一流的測(cè)試能發(fā)現(xiàn)未發(fā)生的故障;二流的測(cè)試能快速定位故障的發(fā)生點(diǎn);三流的測(cè)試則疲于奔命,一直跟在故障后面進(jìn)行功能回歸。
JAVA中常用的單元測(cè)試工具

JUnit/JUnit5

https://junit.org/junit5/

junit是老牌測(cè)試框架了,也是目前引用最廣泛的一個(gè)框架。當(dāng)前已經(jīng)更新到Junit5,功能更強(qiáng)大。

class StandardTests {

  @BeforeAll
  static void initAll() {
  }

  @BeforeEach
  void init() {
  }

  @Test
  void succeedingTest() {
  }

  @Test
  void failingTest() {
    fail("a failing test");
  }

  @Test
  @Disabled("for demonstration purposes")
  void skippedTest() {
    // not executed
  }

  @Test
  void abortedTest() {
    assumeTrue("abc".contains("Z"));
    fail("test should have been aborted");
  }

  @AfterEach
  void tearDown() {
  }

  @AfterAll
  static void tearDownAll() {
  }

}

assertj

https://assertj.github.io/doc/

一個(gè)功能強(qiáng)悍的斷言工具,支持各種斷言方式

// entry point for all assertThat methods and utility methods (e.g. entry)
import static org.assertj.core.api.Assertions.*;

// basic assertions
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron);

// chaining string specific assertions
assertThat(frodo.getName()).startsWith("Fro")
              .endsWith("do")
              .isEqualToIgnoringCase("frodo");

// collection specific assertions (there are plenty more)
// in the examples below fellowshipOfTheRing is a List<TolkienCharacter>
assertThat(fellowshipOfTheRing).hasSize(9)
                .contains(frodo, sam)
                .doesNotContain(sauron);

// as() is used to describe the test and will be shown before the error message
assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33);

// Java 8 exception assertion, standard style ...
assertThatThrownBy(() -> { throw new Exception("boom!"); }).hasMessage("boom!");
// ... or BDD style
Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
assertThat(thrown).hasMessageContaining("boom");

// using the 'extracting' feature to check fellowshipOfTheRing character's names (Java 7)
assertThat(fellowshipOfTheRing).extracting("name")
                .contains("Boromir", "Gandalf", "Frodo", "Legolas")
// same thing using a Java 8 method reference
assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getName)
                .doesNotContain("Sauron", "Elrond");

// extracting multiple values at once grouped in tuples (Java 7)
assertThat(fellowshipOfTheRing).extracting("name", "age", "race.name")
                .contains(tuple("Boromir", 37, "Man"),
                     tuple("Sam", 38, "Hobbit"),
                     tuple("Legolas", 1000, "Elf"));

// filtering a collection before asserting in Java 7 ... 
assertThat(fellowshipOfTheRing).filteredOn("race", HOBBIT)
                .containsOnly(sam, frodo, pippin, merry);
// ... or in Java 8
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
                .containsOnly(aragorn, frodo, legolas, boromir);

// combining filtering and extraction (yes we can)
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
                .containsOnly(aragorn, frodo, legolas, boromir)
                .extracting(character -> character.getRace().getName())
                .contains("Hobbit", "Elf", "Man");

// and many more assertions: iterable, stream, array, map, dates (java 7 and 8), path, file, numbers, predicate, optional ...

Mockito

https://site.mockito.org/

一個(gè)單元測(cè)試中的Mock工具,可以很靈活的創(chuàng)建對(duì)象,配合單元測(cè)試。

// You can mock concrete classes and interfaces
TrainSeats seats = mock(TrainSeats.class);

// stubbing appears before the actual execution
when(seats.book(Seat.near(WINDOW).in(FIRST_CLASS))).thenReturn(BOOKED);

// the following prints "BOOKED"
System.out.println(seats.book(Seat.near(WINDOW).in(FIRST_CLASS)));

// the following prints "null" because 
// .book(Seat.near(AISLE).in(FIRST_CLASS))) was not stubbed
System.out.println(seats.book(Seat.near(AISLE).in(FIRST_CLASS)));

// the following verification passes because 
// .book(Seat.near(WINDOW).in(FIRST_CLASS)) has been invoked
verify(seats).book(Seat.near(WINDOW).in(FIRST_CLASS));

// the following verification fails because 
// .book(Seat.in(SECOND_CLASS)) has not been invoked
verify(seats).book(Seat.in(SECOND_CLASS));

其他

對(duì)于業(yè)務(wù)代碼,有時(shí)單元測(cè)試并不方便,因?yàn)槊看螁?dòng)成本過高。可以使用適當(dāng)?shù)膯卧獪y(cè)試方式,比如可以提供一個(gè)測(cè)試接口,利用IDE的熱部署功能實(shí)現(xiàn)不重啟及時(shí)修改代碼。

但是對(duì)于非業(yè)務(wù)性代碼,進(jìn)行單元測(cè)試時(shí)非常有必要的,可以更早的發(fā)現(xiàn)代碼中的問題,同時(shí)也可以檢驗(yàn)程序的解耦性。

良好的代碼設(shè)計(jì)在單元測(cè)試時(shí)會(huì)更方便,反之緊耦合的設(shè)計(jì)會(huì)給單元測(cè)試帶來很大的困擾。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI