Code Review Videos > Java > How To Run Only Tagged Tests in jUnit

How To Run Only Tagged Tests in jUnit

Recently I’ve started on the Java track over at Exercism. As practically a complete beginner to Java, I am still finding my way around the tooling, and in particular, IntelliJ IDE. As part of the Annalyn’s Infiltration exercise I was provided with 30 failing jUnit tests, where each of the tests is tagged like so:

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

public class AnnalynsInfiltrationTest {

    @DisplayName("The canFastAttack method returns false when knight is awake")
    public void cannot_execute_fast_attack_if_knight_is_awake() {
        boolean knightIsAwake = true;

    @DisplayName("The canFastAttack method returns true when knight is sleeping")
    public void can_execute_fast_attack_if_knight_is_sleeping() {
        boolean knightIsAwake = false;

    @DisplayName("The canSpy method returns false when everyone is sleeping")
    public void cannot_spy_if_everyone_is_sleeping() {
        boolean knightIsAwake = false;
        boolean archerIsAwake = false;
        boolean prisonerIsAwake = false;
        assertThat(AnnalynsInfiltration.canSpy(knightIsAwake, archerIsAwake, prisonerIsAwake)).isFalse();
    }Code language: Java (java)

Notice how each test is annotated with, amongst other things, a tag like @Tag("task:1").

For the first few tests I was alright. I could either run one at a time by clicking the little green ‘play’ icon next to each test, which I did early on in the exercise:

intellij run one junit test at once

And then as I progressed I decided I wanted to run all of them and see how I was getting on.

That is easy also, just click the green play icon on line 7 – for public class AnnalynsInfiltrationTest {:

intellij run all junit tests at once

But here in lies the problem.

With 30 tests, I could either only see some of them, or I could drag the Run section up and see all of them, but then lose two thirds of my IDE code editor window.

As I really knew the first three tasks in the exercise were all passing their respective jUnit tests, and I wasn’t going to modifying that code, it seemed to make logical sense to me to only run the tests tagged with @Tag("task:4"):

junit tests marked with a specific tag

But how the heck to do this from the IDE?

Well, fortunately for you, I will share how to do this. As for a Java newb like myself, this was not particularly obvious at all.

Create A jUnit Run Configuration in IntelliJ IDE

The ‘secret’ is to create a new Run configuration from inside IntelliJ IDE.

At the top of your IDE you should see a little bar with the build, play, debug, and other icons.

Click the drop down box that has the name of your file (may be something else for you, not sure) in it.

Then click ‘Edit Configurations…‘:

intellij edit run configurations menu

This should pop up a new box with your current run configurations in there.

Yours may be different, I have been heavily meddling with mine:

intellij run/debug configurations window

From here, click the + sign in the top left corner.

You need to select ‘JUnit‘ from this menu:

intellij add new junit run configuration

Then fill in the box as follows:

intellij junit run configuration for tags

You can name this configuration anything you like.

I went with : Tags (task:1)

Most of this was filled in for me.

The only thing I needed to change was the drop down box in the ‘Build and run‘ section from ‘Class‘ to ‘Tags‘:

intellij build and run class tags

I selected:

Tags and added the tag of task:1 to exactly match one of the named tags in my current file.

This should not be file specific. If I had other test files in the project with that tag, this run config should match those also.

Then click ‘Apply‘ and ‘OK‘ to save and close the setup.

Now you can run this particular config by clicking the play icon at the top once more:

intellij run specific config

So now I get only the tests that match @Tag("task:1"):

intellij junit tagged test output

Pretty neat.

It would be super nice to have a more elegant solution to this problem. Maybe one exists, I’m not sure.

As it stands, this feels like a fair few steps on a short lived project like an Exercism exercise, but I guess it has far more value in a real world project.

Handy to know, anyway.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.