About Mohammad Meraj Zia

I did my Engineering in Information Technology from IET, Lucknow, India. Currently doing MSc in Information Technology from Derby University. I have worked in Java/J2EE domain for the last 10 years. Have good understanding of Payment and Finance domains.

Want to master IntelliJ IDEA?

In order to help you master IntelliJ IDEA, we have compiled a kick-ass guide with all the basic features of the popular IDE! Besides studying them online you may download the eBook in PDF format!

Email address:

Leave this field empty if you're human:

1. Introduction

IntelliJ in a Java Integrated Development Environment (IDE) used for developing software programs. Now you can use IntelliJ for other programming languages as well. It is developed by JetBrains (formerly known as IntelliJ), and is available as an Apache 2 Licensed community edition, and in a proprietary commercial edition. Both can be used for commercial development. Software testing is a process of executing a program or application with the intent of finding the software bugs. It can also be stated as the process of validating and verifying that a software program or application or product meets the business and technical requirements that guided it’s design and development.

Test Coverage is an important part in Software testing and Software maintenance and it is the measure of the effectiveness of the testing by providing data on different items. Amount of testing performed by a set of test cases is called Test Coverage. By amount of testing we mean what parts of the application program are exercised when we run a test suite. In other words, test coverage is defined as a technique which determines whether our test cases are actually covering the application code and how much code is exercised when we run those test cases.

2. Coverage Settings

In this section we will learn about the different setting which we can use for calculating the code coverage. To change the settings go to File | Settings | Build, Execution, Deployment | Coverage for Windows and Linux, IntelliJ IDEA | Preferences | Build, Execution, Deployment | Coverage for macOS.

Here you can change the settings. Below we will describe the options available and what they mean.

Show options before applying coverage to the editor – This option should be chosen if you want to show the Code Coverage dialog every time you launch a new run configuration with code coverage. The coverage options dialog is displayed, when different coverage data have been produced.

Do not apply collected coverage – This option should be chosen if you want to cancel applying the new code coverage results.

Replace active suites with the new one – This option should be chosen if you have the active suites replaced with the new one every time you launch a new run configuration with code coverage.

Add to active suites – Choose this option to have the new code coverage suite added to the active suites every time you launch a new run configuration with code coverage.

Activate coverage view – Select this check box to have the Coverage tool window opened automatically when an application or test is run with coverage.

3. Example

In this section we will create a simple class then will write a simple test for it. We will run the test with the coverage option to see how good out tests are. First we will see how to create a simple Maven project in IntelliJ.

3.1 Create Maven Project

To create a maven project go to File => New => Project and choose Maven.

Figure 1. Maven Project

Click Next. Give the GroupId and ArtifactId name. For this example we will choose Group Id as com.javacodegeeks and Artifact id as code-coverage.

Figure 2. Group and Artifact Id

Click Next. Choose a project name and location and click Finish. Intellij will create a basic structure of the maven project for you.

3.2 Java Class

In this section we will write a simple java class. The class will have only one simple method with an if-else statement. This method will be used to demonstrate the working of testing coverage. Go to src => main => java and right click on the java folder. Choose New => package. Give the package name click OK. Now right click on the package we just created and choose New => Java Class. Give the class name and click OK. Below is the representation of the class used in this example:

3.3 Test Class

In this section we will write the unit test class for testing the Solution class. Go to src/test/java, right click and choose New => package. Give the package name as com.javacodegeeks. You can choose some other package but make sure it’s good practice to keep the package name of the test class same as the class been tested. For writing this test we need to define an extra dependency in out pom file. Below is what the POM file will look like:

Now we will write the test class. We will make use of three classes/interfaces of junit framework to write the test. The Assert class has a set of assertion methods useful for writing tests. We will also make use of the @Before annotation which is used to initialize our Testing class. When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the org.junit.Test method. The @Before methods of superclasses will be run before those of the current class, unless they are overridden in the current class. No other ordering is defined.

The @Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

In the coverage window you can see the result. It will display what percentage of the code has been covered by the test. You can see the the coverage result on class, methods or line basis. For our example we see that the Method percentage was 100% as there was only one method but the Line percentage was 83% because we didn’t write the test to cover the else condition.

Figure 4. Coverage results

Viewing code coverage helps you detect pieces of your source code that are not affected by simulation. If you close the above window by mistake you can see the coverage data by going to Analyze => Show Coverage Data and then select the coverage Suite. IntelliJ also displays the result in the Project window. This is very useful as you can clearly see which module/package has the worst code coverage and then can write tests for them.

Figure 5. Project coverage

You can also generate the code coverage report from the Coverage window. The coverage report in generated as HTML and will look similar to below:

Figure 6. Coverage Report

5. Conclusion

In this article we learned about IntelliJ and Testing, what it is and why it’s important. Then we looked at what Code/Test Coverage is and why it’s an important tool to have. We discussed about the various code coverage settings available in IntelliJ. Then we demonstrated the code coverage generation by using a simple example. We discussed how to write a simple test case and how to run it with coverage option on. We saw how to interpret the result displayed in the coverage window. In the end we also discussed how to generate an HTML coverage report using IntelliJ.

Leave a Reply

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.