Running tests in IntelliJ IDEA in junit

In this post, I am going tell you how to write JUnit tests in Intellij IDEA. IntelliJ IDEA provides an excellent support to JUnit tests. JUnit plug-in is by default installed in IntelliJ IDEA. Writing and executing JUnit tests is very simple in IntelliJ IDEA. You need to create a simple maven or gradle project and add JUnit dependency.

Adding JUnit dependency

If you are using Maven project, use below dependency in POM file.
 
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

If you are using Gradle project, add below lines in build.gradle file.
 
apply plugin: 'java'
dependencies {
testCompile 'junit:junit:4.12'
}

Writing JUnit Tests

We have already seen how to write simple JUnit tests.

Executing JUnit Tests in IntelliJ IDEA

We can execute JUnit tests in Intellij IDEA by right clicking on a test class and then selecting Run option as shown in below image.You can also use Ctrl+Shift+F10 shortcut key to run current JUnit test. To run Junit tests in specific package, you can right click on that package and click on run. Similarly to run all tests in all packages, you can right click on Java directory and click on Run all tests.

Viewing the JUnit test reports in IntelliJ IDEA

After running the JUnit tests, IntelliJ IDEA creates a beautiful HTML report. Below images show how to view JUnit HTML report generated by IntelliJ IDEA. Below image shows sample output of Junit test execution. To export the report in HTML format, click on icon highlighted in yellow color.Below image shows sample JUnit test execution report generated by Intellij IDEA.If you are using a maven project, you can also execute JUnit tests using maven commands.

Web development and Automation testing

solutions delivered!!