Java单元測试工具JUnit 5新特性一览

2023-06-07,,

Java单元试工具JUnit 5新特性一览

作者:chszs,未经博主同意不得转载。

经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs

JUnit是最流行的开源Java单元測试工具。眼下它的稳定版是4.12版。JUnit 4是2005年推出的,它基于Java 5的凝视、反射等特性开发,距今已经超过十年了,受眼下Java 8以及Lambda表达式等的影响,JUnit团队决定推出JUnit 5版。

眼下JUnit 5已经推出了5.0.0 Alpha版,见: https://t.co/Mb12F3WF4A

一、JUnit 5的更新情况

JUnit 5全然使用当前的Java 8重写了全部代码。因此JUnit 5的运行条件是Java 8环境。

JUnit 5同意在断言中使用Lambda表达式。这个特性能够从开源的断言库AssertJ中能够看到。

AssertJ库见: http://joel-costigliola.github.io/assertj/

与JUnit 4不同,JUnit 5不再是单个库,而是模块化结构的集合,整个API分成了:自己的模块、引擎、launcher、针对Gradle和Surefire的集成模块。

JUnit团队还发起了名为Open Test Alliance for the JVM的活动,见: https://github.com/ota4j-team/opentest4j

JUnit 5的測试看上去与JUnit 4相同:相同是创建类。加入測试方法,使用@Test凝视。

可是。JUnit 5还提供了全新的一套凝视集合,并且断言方法从JUnit 4的org.junit.Assert包移到了JUnit 5的org.junit.gen5.api.Assertions包。

比方:

import org.junit.gen5.api.Assertions;
import org.junit.gen5.api.Test;
public class Test1 {
@Test
public void test() {
Assertions.assertEquals(3 * 6, 18);
Assertions.assertTrue(5 > 4);
}
}

二、JUnit 5断言

JUnit 5的断言方法与JUnit 4类似,断言类提供了assertTrue、assertEquals、assertNull、assertSame以及相反的断言方法。不同之处在于JUnit 5的断言方法支持Lambda表达式。并且另一个名为分组断言(Grouped Assertions)的新特性。

分组断言同意运行一组断言,且会一起报告。

要记得在JUnit 4中,我们被告诫不要在一个測试中放入多个断言,以避免某些断言没有得到运行。如今,在JUnit 5中使用分组断言就无需再顾虑这个避讳了。

对JUnit 4的另一个改进是断言预期的异常。不再是曾经那种把预期的异常类型放入@Test凝视,或者是用try-catch包裹代码,JUnit 5使用assertThrows和equalsThrows断言。

以下看看断言的样例:

public class Test2 {
@Test
public void lambdaExpressions() {
// lambda expression for condition
assertTrue(() -> "".isEmpty(), "string should be empty");
// lambda expression for assertion message
assertEquals("foo", "foo", () -> "message is lazily evaluated");
}
@Test
public void groupedAssertions() {
Dimension dim = new Dimension(800, 600);
assertAll("dimension",
() -> assertTrue(dim.getWidth() == 800, "width"),
() -> assertTrue(dim.getHeight() == 600, "height"));
}
@Test
public void exceptions() {
// assert exception type
assertThrows(RuntimeException.class, () -> {
throw new NullPointerException();
});
// assert on the expected exception
Throwable exception = expectThrows(RuntimeException.class, () -> {
throw new NullPointerException("should not be null");
});
assertEquals("should not be null", exception.getMessage());
}
}

三、如果、标签和禁止測试

如果、标签和禁止測试是JUnit 4的特性,在JUnit 5中仍然得以保留。

不同的是如果中也支持Lambda表达式,如果的思想是如果如果条件没有得到满足。那么跳过測试运行。

标签Tags等同于JUnit 4的測试分类的概念,能够对測试类和方法进行分类。

JUnit 4禁止測试使用了@Ignore凝视。而在JUnit 5中则使用@Disabled凝视。

public class Test3 {
@Test
@Disabled
public void disabledTest() {
// ...
}
@Test
@Tag("jenkins")
public void jenkinsOnly() {
// ...
} @Test
public void windowsOnly() {
Assumptions.assumeTrue(System.getenv("OS").startsWith("Windows"));
// ...
}
}

四、扩展模型

JUnit 5提供了一套新的扩展API。代替了曾经的@RunWith和@Rule扩展机制。JUnit 4的測试类被限制到仅有一个Runner上。而新的扩展模型则同意一个类或方法keyii注冊到多种扩展。

@ExtendWith(MockitoExtension.class)
@ExtendWith(CdiUnitExtension.class)
public class Test4 {
@Test
@DisplayName("awesome test")
void dependencyInjection(TestInfo testInfo) {
assertEquals("awesome test", testInfo.getDisplayName());
}
}

JUnit 5内建的扩展还支持方法级的依赖注入。

支持Hamcrest匹配和AssertJ断言库

JUnit 5支持Hamcrest匹配和AssertJ断言库,能够用它们来代替JUnit 5的方法。

public class Test5 {
@Test
public void emptyString() {
// JUnit 5
org.junit.gen5.api.Assertions.assertTrue("".isEmpty()); // AssertJ
org.assertj.core.api.Assertions.assertThat("").isEmpty(); // Hamcrest
org.hamcrest.MatcherAssert.assertThat("", isEmptyString());
}
}

JUnit 5的主页见: https://github.com/junit-team/junit5

有兴趣的朋友能够关注。

Java单元測试工具JUnit 5新特性一览的相关教程结束。

《Java单元測试工具JUnit 5新特性一览.doc》

下载本文的Word格式文档,以方便收藏与打印。