Explain Codes LogoExplain Codes Logo

Create Test Class in IntelliJ

java
intellij
test-class
maven
Nikita BarsukovbyNikita Barsukov·Feb 3, 2025
TLDR

To quickly generate a Test Class in IntelliJ IDEA, follow these steps: Right-click on the class file -> Choose Generate (Alt+Insert for Windows/Linux, Cmd+N for Mac) -> Select Test. Select your desired testing framework like JUnit, and the methods you need to test. IntelliJ will generate a skeleton test class for you:

public class CalculatorTest { @Test public void addition_isCorrect() { // Running this test is as satisfying as the sound of a fresh can of cola opening. assertEquals(4, new Calculator().add(2, 2)); } }

Replace Calculator with your class name and addition_isCorrect with your testing condition.

Boost your productivity with handy shortcuts

A few tips to supercharge your testing process in IntelliJ:

Using Maven? IntelliJ has got your back

IntelliJ acknowledges the Maven project structure. It will automatically place the new test classes in src/test/java when using the Navigate -> Test shortcut. If tests aren't appearing in the right directory, double-check your project's source settings and modify your POM file if necessary.

Set up your cursor before diving in

Before you press the Navigate -> Test shortcut (Ctrl+Shift+T or Shift+Cmd+T on Mac), place your cursor within the class file you intend to test. This provides IntelliJ the necessary context to streamline the creation of your test class template.

Efficiently create your test class using shortcuts

IntelliJ's Navigate -> Test shortcut creates or suggests a corresponding test class, including the selection of your testing framework. No more manual creation of test classes!

Organize your test classes in the right directories

If your project lacks a dedicated test directory, IntelliJ is helpless. Right-click in your project panel, add a "test" directory and mark that as the Test Sources Root. Voila! IntelliJ now knows where to house the test classes.

Mastering the art of a solid test class

Remember, creating a test class involves more than just a few clicks. It's about best practices and preparation for various scenarios. Here's how to level up:

Select the appropriate framework

When IntelliJ prompts for a testing framework selection, make a considered choice between options like JUnit or TestNG. Select one that aligns with your team's preferences and project demands.

Regularly refactor your tests

Delivering a feature doesn't stop at writing the production code. The quality of your tests matter too. Utilize IntelliJ's Refactor options to keep your test code clean, efficient and performant.

CI is integral to your workflow

Ensure your test class creation process aligns seamlessly with your Continuous Integration pipeline. Take care to correctly place and name the tests to suit with your Gradle or Maven build scripts.

Solving test generation woes

If unable to generate a test class, cross-check the following:

  1. Structure: Is the source set valid, reflecting src/main/java and src/test/java? If unsure, run to the rescue!
  2. Dependencies: Are the testing framework dependencies correctly declared in your build files? Check, before they wreak havoc.
  3. Settings: Visit File -> Project Structure for any possible setting mismatches. IntelliJ won't mind a little peek.