<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
</plugin>
JUnit5的新功能 (JUnit5 New Features)
It requires Java 8 or higher at runtime. But one can still test code which is compiled using previous Java versions. There are various new features got introduced in it.
运行时需要Java 8或更高版本。 但是仍然可以测试使用以前的Java版本编译的代码。 其中引入了各种新功能。
JUnit注释 (JUnit Annotations)
Listed below are some commonly used annotations provided in it:
下面列出了其中提供的一些常用注释:
Annotation | Description |
---|---|
@Test | Denotes a test method |
@DisplayName | Declares a custom display name for the test class or test method |
@BeforeEach | Denotes that the annotated method should be executed before each test method |
@AfterEach | Denotes that the annotated method should be executed after each test method |
@BeforeAll | Denotes that the annotated method should be executed before all test methods |
@AfterAll | Denotes that the annotated method should be executed after all test methods |
@Disable | Used to disable a test class or test method |
@Nested | Denotes that the annotated class is a nested, non-static test class |
@Tag | Declare tags for filtering tests |
@ExtendWith | Register custom extensions |
注解 | 描述 |
---|---|
@测试 | 表示测试方法 |
@显示名称 | 声明测试类或测试方法的自定义显示名称 |
@BeforeEach | 表示带注释的方法应在每个测试方法之前执行 |
@AfterEach | 表示带注释的方法应在每个测试方法之后执行 |
@BeforeAll | 表示带注释的方法应在所有测试方法之前执行 |
@毕竟 | 表示带注释的方法应在所有测试方法之后执行 |
@禁用 | 用于禁用测试类或测试方法 |
@嵌套 | 表示带注释的类是嵌套的非静态测试类 |
@标签 | 声明标签以过滤测试 |
@ExtendWith | 注册自定义扩展 |
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class JUnit5Sample1Test {
@BeforeAll
static void beforeAll() {
("**--- Executed once before all test methods in this class ---**");
}
@BeforeEach
void beforeEach() {
("**--- Executed before each test method in this class ---**");
}
@Test
void testMethod1() {
("**--- Test method1 executed ---**");
}
@DisplayName("Test method2 with condition")
@Test
void testMethod2() {
("**--- Test method2 executed ---**");
}
@Test
@Disabled("implementation pending")
void testMethod3() {
("**--- Test method3 executed ---**");
}
@AfterEach
void afterEach() {
("**--- Executed after each test method in this class ---**");
}
@AfterAll
static void afterAll() {
("**--- Executed once after all test methods in this class ---**");
}
}
We can run above JUnit test class in Eclipse -> Run As -> JUnit Test.
我们可以在Eclipse-> Run As-> JUnit Test中的 JUnit测试类之上运行 。
JUnit断言 (JUnit Assertions)
Every test method must be evaluated against condition to true using assertions so that the test can continue to execute. JUnit Jupiter assertions are kept in the class. All of the methods are static.
必须使用断言将每个测试方法的条件评估为true,以便测试可以继续执行。 JUnit Jupiter断言保存在类中。 所有方法都是静态的。
Assertion | Description |
---|---|
assertEquals(expected, actual) | Fails when expected does not equal actual |
assertFalse(expression) | Fails when expression is not false |
assertNull(actual) | Fails when actual is not null |
assertNotNull(actual) | Fails when actual is null |
assertAll() | Group many assertions and every assertion is executed even if one or more of them fails |
assertTrue(expression) | Fails if expression is not true |
assertThrows() | Class to be tested is expected to throw an exception |
断言 | 描述 |
---|---|
assertEquals(预期的,实际的) | 预期不等于实际时失败 |
assertFalse(表达式) | 表达式不为假时失败 |
assertNull(实际) | 实际不为空时失败 |
assertNotNull(实际) | 当real为null时失败 |
assertAll() | 对许多断言进行分组,并且每个断言都会执行,即使其中一个或多个失败 |
assertTrue(表达式) | 如果表达式不正确则失败 |
assertThrows() | 预期要测试的类将引发异常 |
@Test
void testAssertEqual() {
assertEquals("ABC", "ABC");
assertEquals(20, 20, "optional assertion message");
assertEquals(2 + 2, 4);
}
@Test
void testAssertFalse() {
assertFalse("FirstName".length() == 10);
assertFalse(10 > 20, "assertion message");
}
@Test
void testAssertNull() {
String str1 = null;
String str2 = "abc";
assertNull(str1);
assertNotNull(str2);
}
@Test
void testAssertAll() {
String str1 = "abc";
String str2 = "pqr";
String str3 = "xyz";
assertAll("numbers",
() -> assertEquals(str1,"abc"),
() -> assertEquals(str2,"pqr"),
() -> assertEquals(str3,"xyz")
);
//uncomment below code and understand each assert execution
/*assertAll("numbers",
() -> assertEquals(str1,"abc"),
() -> assertEquals(str2,"pqr1"),
() -> assertEquals(str3,"xyz1")
);*/
}
@Test
void testAssertTrue() {
assertTrue("FirstName".startsWith("F"));
assertTrue(10 {
throw new IllegalArgumentException("Illegal Argument Exception occured");
});
assertEquals("Illegal Argument Exception occured", ());
}
JUnit5导入 (JUnit5 Imports)
Its test classes need import statement and not
. Also, the test methods need not be a public and local package.
它的测试类需要导入语句,而不是
。 同样,测试方法不必是公共和本地程序包。
import ;
JUnit5假设 (JUnit5 Assumptions)
Assumptions are static methods in the class. They will execute a test only when the specified condition met otherwise test will be aborted. The aborted test will not cause build failure. When an assumption fails,
org.
is thrown and the test is skipped.
假设是类中的静态方法。 他们仅在满足指定条件时执行测试,否则测试将中止。 中止的测试不会导致构建失败。 当假设失败时,将抛出
org.
并跳过测试。
Assumptions | Description |
---|---|
assumeTrue | Execute the body of lamda when the positive condition hold else test will be skipped |
assumeFalse | Execute the body of lamda when the negative condition hold else test will be skipped |
assumingThat | Portion of the test method will execute if an assumption holds true and everything after the lambda will execute irrespective of the assumption in assumingThat() holds |
假设条件 | 描述 |
---|---|
假设是真的 | 当阳性条件成立时执行lamda主体,否则将跳过测试 |
假设为假 | 负条件成立时执行lamda主体,否则将跳过测试 |
假如说 | 如果假设成立,则测试方法的一部分将执行,并且lambda之后的所有内容都将执行,而与前提条件thatthat()成立的情况无关 |
@Test
void testAssumeTrue() {
boolean b = 'A' == 'A';
assumeTrue(b);
assertEquals("Hello", "Hello");
}
@Test
@DisplayName("test executes only on Saturday")
public void testAssumeTrueSaturday() {
LocalDateTime dt = ();
assumeTrue(().getValue() == 6);
("further code will execute only if above assumption holds true");
}
@Test
void testAssumeFalse() {
boolean b = 'A' != 'A';
assumeFalse(b);
assertEquals("Hello", "Hello");
}
@Test
void testAssumeFalseEnvProp() {
("env", "prod");
assumeFalse("dev".equals(("env")));
("further code will execute only if above assumption hold");
}
@Test
void testAssumingThat() {
("env", "test");
assumingThat("test".equals(("env")),
() -> {
assertEquals(10, 10);
("perform below assertions only on the test env");
});
assertEquals(20, 20);
("perform below assertions on all env");
}
JUnit嵌套测试类 (JUnit Nested Test Classes)
Nested tests allow to create nested classes and execute all of its test methods. The inner classes must be non-static. Just annotate inner classes with @Nested and all test methods inside it will be executed.
嵌套测试允许创建嵌套类并执行其所有测试方法。 内部类必须是非静态的。 只需使用@Nested注释内部类,即可执行其中的所有测试方法。
@BeforeAll
static void beforeAll() {
("**--- JUnit5Sample4Test :: beforeAll :: Executed once before all test methods ---**");
}
@BeforeEach
void beforeEach() {
("**--- JUnit5Sample4Test :: beforeEach :: Executed before each test method ---**");
}
@AfterEach
void afterEach() {
("**--- JUnit5Sample4Test :: afterEach :: Executed after each test method ---**");
}
@AfterAll
static void afterAll() {
("**--- JUnit5Sample4Test :: afterAll :: Executed after all test method ---**");
}
@Nested
class InnerClass {
@BeforeEach
void beforeEach() {
("**--- InnerClass :: beforeEach :: Executed before each test method ---**");
}
@AfterEach
void afterEach() {
("**--- InnerClass :: afterEach :: Executed after each test method ---**");
}
@Test
void testMethod1() {
("**--- InnerClass :: testMethod1 :: Executed test method1 ---**");
}
@Nested
class InnerMostClass {
@BeforeEach
void beforeEach() {
("**--- InnerMostClass :: beforeEach :: Executed before each test method ---**");
}
@AfterEach
void afterEach() {
("**--- InnerMostClass :: afterEach :: Executed after each test method ---**");
}
@Test
void testMethod2() {
("**--- InnerMostClass :: testMethod2 :: Executed test method2 ---**");
}
}
}
JUnit测试异常 (JUnit Test Exception)
There are situations in which methods are expected to throw an exception under a specific condition. assertThrows will fail the test if the given method does not throw the specified exception.
在某些情况下,期望方法在特定条件下引发异常。 如果给定方法未引发指定的异常,则assertThrows将使测试失败。
Throwable exception = assertThrows(, () -> {
throw new IllegalArgumentException("Illegal Argument Exception occured");
});
assertEquals("Illegal Argument Exception occured", ());
JUnit测试执行 (JUnit Test Execution)
The unit tests can be executed in many ways, two of the ways are as follows:
单元测试可以通过多种方式执行,其中两种方式如下:
- Use Eclipse IDE Oxygen.3a (4.7.3a) Release and open test file to be executed. Right-click on the file and choose option Runs As followed by JUnit Test 使用Eclipse IDE Oxygen.3a(4.7.3a)发行并打开要执行的测试文件。 右键单击文件,然后选择选项Runs As,然后选择JUnit Test
- Use mvn test command on Windows command prompt 在Windows命令提示符下使用mvn test命令
摘要 (Summary)
We’ve explored JUnit5 and its new features with some examples. We also looked how we can use JUnit annotations, assertions, assumptions, exceptions and write nested test classes.
我们通过一些示例探索了JUnit5及其新功能。 我们还研究了如何使用JUnit批注,断言,假设,异常并编写嵌套的测试类。
翻译自: /20834/junit5-tutorial
junit5教程