JUnit Test Cases are normal java class with methods to be executed by JUnit framework. JUnit 5 is a major upgrade from earlier versions.
JUnit测试用例是普通的Java类,带有要由JUnit框架执行的方法。 JUnit 5是早期版本的主要升级。
在Eclipse中创建JUnit测试用例 (Creating JUnit Test Cases in Eclipse)
Latest Eclipse versions come with built-in support for JUnit test cases creation. We can easily create JUnit test cases class in Eclipse as shown below.
最新的Eclipse版本附带了对JUnit测试用例创建的内置支持。 我们可以在Eclipse中轻松创建JUnit测试用例类,如下所示。
Go to New | JUnit Test Case in your Eclipse.
转到新| Eclipse中的JUnit测试用例 。
In the next popup window, provide the test class name, its package, method stubs to generate etc. Note that Eclipse provides us option to create JUnit 3, JUnit 4 and JUnit Jupiter Test Cases. Unless your project is using the older version of JUnit and you haven’t migrated to the latest versions, it’s better to create the JUnit Jupiter test case.
在下一个弹出窗口中,提供测试类名称,其包,要生成的方法存根等。请注意,Eclipse提供了创建JUnit 3,JUnit 4和JUnit Jupiter测试用例的选项。 除非您的项目使用的是JUnit的较早版本,并且尚未迁移到最新版本,否则最好创建JUnit Jupiter测试用例。
Once you click on “Finish” button, the test case class will be generated. You can run this class by going to Run | Run As | JUnit Test. You can also reach this menu by right-clicking in the Editor window or by selecting the class and then right click on it.
单击“完成”按钮后,将生成测试用例类。 您可以通过运行|运行该类。 运行方式| JUnit测试 。 您也可以通过在“编辑器”窗口中右键单击或选择该类,然后在其上单击鼠标右键来访问此菜单。
JUnit Eclipse运行配置 (JUnit Eclipse Run Configurations)
You can also create run configurations to execute JUnit test cases. Run configurations are helpful in running multiple test classes at once by selecting the package, or selecting only a few methods to run in the test class.
您还可以创建运行配置以执行JUnit测试用例。 通过选择程序包或仅选择一些方法在测试类中运行,运行配置有助于一次运行多个测试类。
When you run a JUnit test, a sample run configuration is automatically created. So you can just edit them as per your requirements.
运行JUnit测试时,将自动创建一个示例运行配置。 因此,您可以根据需要进行编辑。
使用Maven Build执行JUnit测试用例 (JUnit Test Cases Execution with Maven Build)
If you want your JUnit 5 test cases to be executed with maven build, you will have to configure maven-surefire-plugin
with junit-platform-surefire-provider
dependencies.
如果要使用Maven构建执行JUnit 5测试用例,则必须使用junit-platform-surefire-provider
依赖项配置maven-surefire-plugin
。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Here is a simple JUnit test class I have created in my example project.
这是我在示例项目中创建的简单JUnit测试类。
package com.journaldev.annotations;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.journaldev.utils.MyUtils;
class MyUtilsTest {
@BeforeAll
static void setUpBeforeClass() throws Exception {
System.out.println("Set Up Before Class - @BeforeAll");
}
@AfterAll
static void tearDownAfterClass() throws Exception {
System.out.println("Tear Down After Class - @AfterAll");
}
@BeforeEach
void setUp() throws Exception {
System.out.println("Set Up @BeforeEach");
}
@AfterEach
void tearDown() throws Exception {
System.out.println("Tear Down @AfterEach");
}
@Test
void test_add() {
assertEquals(10, MyUtils.add(5, 5));
}
@Test
void test_reverse() {
assertEquals("cba", MyUtils.reverse("abc"));
}
}
When the mvn test
command is executed, it produces the following snippet for our JUnit test class execution.
当执行mvn test
命令时,它将为我们的JUnit测试类执行生成以下代码段。
[INFO] Running com.journaldev.annotations.MyUtilsTest
Set Up Before Class - @BeforeAll
Set Up @BeforeEach
Tear Down @AfterEach
Set Up @BeforeEach
Tear Down @AfterEach
Tear Down After Class - @AfterAll
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.journaldev.annotations.MyUtilsTest
If you want to skip test cases execution, you can run the following command:
如果要跳过测试用例的执行,可以运行以下命令:
mvn clean install -Dmaven.test.skip=true
摘要 (Summary)
JUnit test cases help us in unit testing our code. Eclipse IDE provides great built-in support for JUnit test classes creation and execution. We also learned how to configure our maven project to execute JUnit Jupiter test cases from command line maven build.
JUnit测试用例可帮助我们对代码进行单元测试。 Eclipse IDE为JUnit测试类的创建和执行提供了强大的内置支持。 我们还学习了如何从命令行maven构建中配置maven项目以执行JUnit Jupiter测试用例。
翻译自: https://www.journaldev.com/21621/junit-test-cases-eclipse-maven