Intellij Idea 简明教程

Intellij Idea - Unit Testing

单元测试在软件开发中起着重要作用。IntelliJ 支持各种单元测试框架,如 JUnit、TestNG、Spock 等。在本章中,我们将使用 JUnit3。

Unit testing plays an important role in software development. IntelliJ supports various unit testing frameworks like JUnit, TestNG, Spock and many more. In this chapter, we are going to use JUnit3.

Create Unit test

在本节中,我们将学习如何创建单元测试。按照以下步骤创建测试 −

In this section, we will learn how to create a Unit Test. Follow these steps to create the test −

  1. Select the Navigate → Test option.

  2. A dialog box will appear wherein, you have to select Create New Test.

  3. Follow the on-screen instructions to continue −

create unit test
  1. Provide the details about the test like testing library, class details, setUp, tearDown methods and so on.

tear down methods
  1. Click on the OK button to continue.

  2. A test class will be created. Initially it may fail to resolve some symbols. Navigate the cursor to the error line, it will show the hint symbol.

  3. Select the appropriate option from the dropdown menu. We have selected the Add library ‘junit.jar!’ to classpath option −

classpath option

你可以根据业务要求在每个测试中添加逻辑。为了简单起见,我已将此项保留为空。

You can add logic inside each test according to your business requirement. I have kept it empty for simplicity.

Run Unit Test

按照以下步骤来运行单元测试 -

Follow these steps to run unit test −

  1. Select unit test class from the Editor window.

  2. Navigate to the Run menu and select the Run option.

  3. The following result will be generated

run option