JUnit5常用注解的使用
注解(Annotations)是JUnit的標(biāo)志性技術(shù),本文就來對它的20個(gè)注解,以及元注解和組合注解進(jìn)行學(xué)習(xí)。
20個(gè)注解
在org.junit.jupiter.api包中定義了這些注解,它們分別是:
@Test 測試方法,可以直接運(yùn)行。
@ParameterizedTest 參數(shù)化測試,比如:
@ParameterizedTest @ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" }) void palindromes(String candidate) { assertTrue(StringUtils.isPalindrome(candidate)); }
@RepeatedTest 重復(fù)測試,比如:
@RepeatedTest(10) void repeatedTest() { // ... }
@TestFactory 測試工廠,專門生成測試方法,比如:
import org.junit.jupiter.api.DynamicTest; @TestFactory Collection<DynamicTest> dynamicTestsFromCollection() { return Arrays.asList( dynamicTest("1st dynamic test", () -> assertTrue(isPalindrome("madam"))), dynamicTest("2nd dynamic test", () -> assertEquals(4, calculator.multiply(2, 2))) ); }
@TestTemplate 測試模板,比如:
final List<String> fruits = Arrays.asList("apple", "banana", "lemon"); @TestTemplate @ExtendWith(MyTestTemplateInvocationContextProvider.class) void testTemplate(String fruit) { assertTrue(fruits.contains(fruit)); } public class MyTestTemplateInvocationContextProvider implements TestTemplateInvocationContextProvider { @Override public boolean supportsTestTemplate(ExtensionContext context) { return true; } @Override public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts( ExtensionContext context) { return Stream.of(invocationContext("apple"), invocationContext("banana")); } }
@TestTemplate必須注冊一個(gè)TestTemplateInvocationContextProvider,它的用法跟@Test類似。
@TestMethodOrder 指定測試順序,比如:
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; @TestMethodOrder(OrderAnnotation.class) class OrderedTestsDemo { @Test @Order(1) void nullValues() { // perform assertions against null values } @Test @Order(2) void emptyValues() { // perform assertions against empty values } @Test @Order(3) void validValues() { // perform assertions against valid values } }
@TestInstance 是否生成多個(gè)測試實(shí)例,默認(rèn)JUnit每個(gè)測試方法生成一個(gè)實(shí)例,使用這個(gè)注解能讓每個(gè)類只生成一個(gè)實(shí)例,比如:
@TestInstance(Lifecycle.PER_CLASS) class TestMethodDemo { @Test void test1() { } @Test void test2() { } @Test void test3() { } }
@DisplayName 自定義測試名字,會(huì)體現(xiàn)在測試報(bào)告中,比如:
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @DisplayName("A special test case") class DisplayNameDemo { @Test @DisplayName("Custom test name containing spaces") void testWithDisplayNameContainingSpaces() { } @Test @DisplayName("╯°□°)╯") void testWithDisplayNameContainingSpecialCharacters() { } @Test @DisplayName("😱") void testWithDisplayNameContainingEmoji() { } }
@DisplayNameGeneration 測試名字統(tǒng)一處理,比如:
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.IndicativeSentencesGeneration; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class DisplayNameGeneratorDemo { @Nested @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) class A_year_is_not_supported { @Test void if_it_is_zero() { } @DisplayName("A negative value for year is not supported by the leap year computation.") @ParameterizedTest(name = "For example, year {0} is not supported.") @ValueSource(ints = { -1, -4 }) void if_it_is_negative(int year) { } } @Nested @IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) class A_year_is_a_leap_year { @Test void if_it_is_divisible_by_4_but_not_by_100() { } @ParameterizedTest(name = "Year {0} is a leap year.") @ValueSource(ints = { 2016, 2020, 2048 }) void if_it_is_one_of_the_following_years(int year) { } } }
@BeforeEach 在每個(gè)@Test, @RepeatedTest, @ParameterizedTest, or @TestFactory之前執(zhí)行。
@AfterEach 在每個(gè)@Test, @RepeatedTest, @ParameterizedTest, or @TestFactory之后執(zhí)行。
@BeforeAll 在所有的@Test, @RepeatedTest, @ParameterizedTest, and @TestFactory之前執(zhí)行。
@AfterAll 在所有的@Test, @RepeatedTest, @ParameterizedTest, and @TestFactory之后執(zhí)行。
@Nested 嵌套測試,一個(gè)類套一個(gè)類,例子參考上面那個(gè)。
@Tag 打標(biāo)簽,相當(dāng)于分組,比如:
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @Tag("fast") @Tag("model") class TaggingDemo { @Test @Tag("taxes") void testingTaxCalculation() { } }
@Disabled 禁用測試,比如:
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @Disabled("Disabled until bug #99 has been fixed") class DisabledClassDemo { @Test void testWillBeSkipped() { } } @Timeout 對于test, test factory, test template, or lifecycle method,如果超時(shí)了就認(rèn)為失敗了,比如: class TimeoutDemo { @BeforeEach @Timeout(5) void setUp() { // fails if execution time exceeds 5 seconds } @Test @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) void failsIfExecutionTimeExceeds100Milliseconds() { // fails if execution time exceeds 100 milliseconds } }
@ExtendWith 注冊擴(kuò)展,比如:
@ExtendWith(RandomParametersExtension.class) @Test void test(@Random int i) { // ... }
JUnit5提供了標(biāo)準(zhǔn)的擴(kuò)展機(jī)制來允許開發(fā)人員對JUnit5的功能進(jìn)行增強(qiáng)。JUnit5提供了很多的標(biāo)準(zhǔn)擴(kuò)展接口,第三方可以直接實(shí)現(xiàn)這些接口來提供自定義的行為。
@RegisterExtension 通過字段注冊擴(kuò)展,比如:
class WebServerDemo { @RegisterExtension static WebServerExtension server = WebServerExtension.builder() .enableSecurity(false) .build(); @Test void getProductList() { WebClient webClient = new WebClient(); String serverUrl = server.getServerUrl(); // Use WebClient to connect to web server using serverUrl and verify response assertEquals(200, webClient.get(serverUrl + "/products").getResponseStatus()); } }
@TempDir 臨時(shí)目錄,比如:
@Test void writeItemsToFile(@TempDir Path tempDir) throws IOException { Path file = tempDir.resolve("test.txt"); new ListWriter(file).write("a", "b", "c"); assertEquals(singletonList("a,b,c"), Files.readAllLines(file)); }
元注解和組合注解
JUnit Jupiter支持元注解,能實(shí)現(xiàn)自定義注解,比如自定義@Fast注解:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.junit.jupiter.api.Tag; @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Tag("fast") public @interface Fast { }
使用:
@Fast @Test void myFastTest() { // ... }
這個(gè)@Fast注解也是組合注解,甚至可以更進(jìn)一步和@Test組合:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Tag("fast") @Test public @interface FastTest { }
只用@FastTest就可以了:
@FastTest void myFastTest() { // ... }
小結(jié)
本文對JUnit20個(gè)主要的注解進(jìn)行了介紹和示例演示,JUnit Jupiter支持元注解,可以自定義注解,也可以把多個(gè)注解組合起來。
參考資料:
https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations
https://vitzhou.gitbooks.io/junit5/content/junit/extension_model.html#概述
到此這篇關(guān)于JUnit5常用注解的使用的文章就介紹到這了,更多相關(guān)JUnit5注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)簡易撲克牌游戲的完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)簡易撲克牌游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04一文詳解如何在控制臺(tái)顯示MyBatis的SQL語句
這篇文章主要為大家介紹了如何在控制臺(tái)顯示MyBatis的SQL語句實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06java實(shí)現(xiàn)馬踏棋盤算法(騎士周游問題)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)馬踏棋盤算法,解決騎士周游問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02git stash 和unstash的使用操作,git unstash failed
這篇文章主要介紹了git stash 和unstash的使用操作,git unstash failed,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等)
這篇文章主要介紹了JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03Java 3種方法實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
這篇文章主要介紹了Java 3種方法實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換,幫助大家利用Java處理數(shù)據(jù),感興趣的朋友可以了解下2020-09-09Netty學(xué)習(xí)教程之Netty與Marshalling結(jié)合發(fā)送對象
Netty是由JBOSS提供的一個(gè)Java開源框架,之前已經(jīng)給大家簡單介紹了一些基礎(chǔ)與使用,下面這篇文章主要給大家介紹了關(guān)于Netty與Marshalling結(jié)合發(fā)送對象的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05