什么是單元測(cè)試
創(chuàng)新互聯(lián)從2013年成立,先為橋西等服務(wù)建站,橋西等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為橋西企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
單元測(cè)試(英語(yǔ):Unit Testing)又稱為模塊測(cè)試, 是針對(duì)程序模塊(軟件設(shè)計(jì)的最小單位)來(lái)進(jìn)行正確性檢驗(yàn)的測(cè)試工作。程序單元是應(yīng)用的最小可測(cè)試部件。在過(guò)程化編程中,一個(gè)單元就是單個(gè)程序、函數(shù)、過(guò)程等;對(duì)于面向?qū)ο缶幊?,最小單元就是方法,包括基類(超類)、抽象類、或者派生類(子類)中的方法?/p>
通常來(lái)說(shuō),程序員每修改一次程序就會(huì)進(jìn)行最少一次單元測(cè)試,在編寫程序的過(guò)程中前后很可能要進(jìn)行多次單元測(cè)試,以證實(shí)程序達(dá)到軟件規(guī)格書要求的工作目標(biāo),沒(méi)有程序錯(cuò)誤;雖然單元測(cè)試不是什么必須的,但也不壞,這牽涉到項(xiàng)目管理的政策決定。
單元測(cè)試的優(yōu)點(diǎn)
優(yōu)質(zhì)的單元測(cè)試可以保障開(kāi)發(fā)質(zhì)量和程序的魯棒性。在大多數(shù)互聯(lián)網(wǎng)企業(yè)中開(kāi)發(fā)工程師在研發(fā)過(guò)程中都會(huì)頻繁地執(zhí)行測(cè)試用例,運(yùn)行失敗的單測(cè)能幫助我們快速排查和定位問(wèn)題 使問(wèn)題在被帶到線上之前完成修復(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)成本過(guò)高??梢允褂眠m當(dāng)?shù)膯卧獪y(cè)試方式,比如可以提供一個(gè)測(cè)試接口,利用IDE的熱部署功能實(shí)現(xiàn)不重啟及時(shí)修改代碼。
但是對(duì)于非業(yè)務(wù)性代碼,進(jìn)行單元測(cè)試時(shí)非常有必要的,可以更早的發(fā)現(xiàn)代碼中的問(wèn)題,同時(shí)也可以檢驗(yàn)程序的解耦性。
良好的代碼設(shè)計(jì)在單元測(cè)試時(shí)會(huì)更方便,反之緊耦合的設(shè)計(jì)會(huì)給單元測(cè)試帶來(lái)很大的困擾。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站標(biāo)題:JAVA中單元測(cè)試的常用方式(小結(jié))
文章起源:http://jinyejixie.com/article12/pgihgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)