On this page:
- Introduction
- Prerequisites
- Creating Your First Test
- Writing the Test Body
- Running the Tests
- Implementing the Code
- Iterate
- Summary
Introduction
Whether you like to write your tests before writing production code, or like to create the tests afterwards,
IntelliJ IDEA makes it easy to create and run unit tests. In this tutorial we’re going to show how to use
IntelliJ IDEA to write tests first
(Test Driven Development or TDD
).
Prerequisites
This tutorial assumes the following prerequisites:
- You have created a Java project in IntelliJ IDEA.
- You have folders for production and test code, either created manually or from when you created the project.
- You have JUnit 4 on the classpath, either by adding a test library, or adding a dependency in Gradle or Maven.
- Your source and test roots are correctly configured - source roots will appear as blue folders, test folders will have a green background.
- You have created the packages you want in your project.
- Right click on the package you want to create the test in and select New | Java Class.
- Enter the test name - given we’re testing using JUnit, this will likely be [Something]Test, for example
MoodAnalyserTest.
- With the cursor placed somewhere inside the class’s curly braces, press Alt+Insert.
- Select Test Method | JUnit 4 from the menu. This will create a
test method with the default template. Fill in a name for the test, press enter and the cursor will end
up in the method body.
You can alter the default test method template - for example, if you wish to change the start of the method name from
testtoshould.
- Write your tests describing what you want to achieve, pressing Alt+Enter on
any classes that don’t exist and selecting “Create class ‘[ClassName]’”. This will give you a minimum
implementation that keeps the compiler happy.
- Continue writing the test body, including names of methods that you need that don’t exist, again you can
use Alt+Enter and select “Create method ‘[methodName]’” to have IntelliJ
IDEA create a bare skeleton method.
As always, you can use IntelliJ IDEA’s refactoring tools to create variables to store results in, and IntelliJ IDEA will import the most appropriate classes for you, if the correct libraries are on the classpath.
- From inside the test, press Ctrl+Shift+F10 to run this individual test.
The results will be shown in the run dialog. The test name will have an icon next to it - either red for an exception, or yellow for an assertion that fails. For either type of failure, a message stating what went wrong is also shown.
- You can navigate to the code being tested using the usual methods - clicking through on the method name, pressing Ctrl+Alt+B while the cursor is on the method name, or pressing Ctrl+Shift+T to switch between the test and the production code.
- Make the change to the method to make the test pass. Often with TDD, the simplest thing that works might be hard-coding your expected value. We will see later how iterating over this process will lead to more realistic production code.
- Re-run the test, using Shift+F10 to re-run the last test.
- See the test pass - the icon next to the test method should go green. If it does not, make the required
changes until the test passes.
- In your test class, use Alt+Insert again to create a new test method.
- Pick a second test case that specifies more requirements of the method you’re testing. Remember that you can use IntelliJ IDEA’s features to create classes and methods to keep the compiler happy.
- Run this second test case, showing that it fails for the correct reason.
- Change the code in the method being tested to make this test pass.
- Re-run both the tests by pressing Ctrl+Shift+F10 inside the test class, not
inside a single method, and see that both tests now pass. If either test fails, make the changes needed
to the code to ensure the tests pass.
Summary
Writing your first test in a test-first style takes a small amount of setup - creating the test class, creating the test methods, and then creating empty implementations of the code that will eventually become production code. IntelliJ IDEA automates a lot of this initial setup.
As you iterate through the process, creating tests and then making the changes required to get those tests to pass, you build up a comprehensive suite of tests for your required functionality, and the simplest solution that will meet these requirements.