Java Development Tool IntelliJ IDEA unit test and code coverage Diagram

Source: Internet
Author: User

Java Development Tool IntelliJ IDEA Tutorial: unit test and code coverage

This article shows how to use IntelliJ IDEA to develop Unit Testing and Analysis coverage.

1. Create a new project

Create a Java project named UnitTestingApp.

2. Create a class for testing.

Create a new class for testing.

The add method sayHello returns the Hello string.

3. Create a test source root directory

To avoid adding tests to the source, you can create a root directory. In this case, the test will be separated from the product code.

Create a test source root directory.

4. Create a test class

IntelliJ IDEA provides a shortcut for Cmd + Shift + T as the navigation between classes and tests. You can also create a test class there.

Select JUnit 4 as the Unit Test Library. IntelliJ IDEA adds the provided Component Library to the module. Select the method for generating setUp and sayHello.

If the project has several source root directories, IDE requires you to select one of them to place a new test class.

After the test class is generated, we can add code for our test method testSayHello.

5. Run the test

Now we can right-click the class and Run 'myclasstest 'for testing, or Run → Edit mappings.

The result is displayed in the Run tool window.

6. Edit test settings

You can adjust the test Run configuration through Run → Edit operations or the labels on the toolbar.

On the Configuration tab, you can select the test to be run. For example, you can run all tests from a class, package, test suite, or even mode. The Fork mode allows the user to run each test in a separate process.

You can adjust the coverage setting in the code override tag. Currently, IntelliJ IDEA supports two measurement coverage engines. By default, it uses its own engine. Of course, you can also choose the JaCoCo engine. You can also select the coverage mode here. Tracing {span {mode increases consumption, but the measurement is more accurate.

7. Coverage

Collect Coverage. You need to Run a test in a specific mode by running 'myclasstest 'with Coverage or using the options on the toolbar.

When at least one test is run in overwrite mode, IDE displays the Coverage data of each package and class in the Project tool window, as well as the Coverage data in the Coverage tool window and editor.

8. Coverage in the editor

If you add another method to MyClass and run the coverage test MyClass, you will find that all codes not covered by the test are highlighted in red. The color of the overwritten code is green. If some code is only covered, it is not yellow.

IntelliJ IDEA Latest Version Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.