Writing & Running JUnits With Intellij & Gradle
2 min readMar 5, 2021
Prerequisites
- You have created a Java project in IntelliJ IDEA.
- You have JUnit 4 or JUnit5 on the classpath, either by adding a test library, or adding a dependency in Gradle.
Creating Your First Test

- In your production code in the editor, place the caret at the class for which you want to create a test, press ⌥⏎ / Alt+Insert, and select Create Test.
- Select Test Method | JUnit 4 from the menu. Select methods that you want to test.

Writing the Test Body

- Write your tests describing what you want to achieve. For example in above code we want to get domain.
Iterate
Developing code is an iterative process.
- Pick a second test case that specifies more requirements of the method you’re testing.
- For example in our case various test cases for the method can be domain is found , not found, host can be null ….
- Continue writing the test bodies, including scenarios as methods that you need.
Running the Tests using Intellij



Running the Tests using Gradle
# Executes all tests in SuffixBasedDomainExtractorTest
gradle test --tests SuffixBasedDomainExtractorTest
# Executes a single specified test in SuffixBasedDomainExtractorTest
gradle test --tests SuffixBasedDomainExtractorTest.testExtractDomainGoogle# Executes all tests in folder
gradle test