DWB
DWB

Reputation: 1554

Running JUnit Tests in IntelliJ with category annotations

I am trying to run all tests with the annotation @FastTests in a module using IntelliJ . I can run all tests in the module using the JUnit configuration with the test kind All In Package and the relevant package name.

When I try to run a single category by choosing the Category test kind JUnit configuration and choose Search For Tests: Across Module Dependencies or Search for Tests: In Single module I get No Tests were found

Is there a way to run JUnit tests with annotated with a single category in intellij?

Cheers

Upvotes: 5

Views: 2387

Answers (1)

lcnicolau
lcnicolau

Reputation: 3878

This works for me using Intellij IDEA 2018.1:

enter image description here

with category marker defined as interface:

public interface FastTests { /* category marker */ }

and test class or method annotated like this:

import cu.nicolau.sircap.nomencladores.FastTests;
import org.junit.Test;
import org.junit.experimental.categories.Category;

public class NomencladorServiceImplTest {    
    @Test
    @Category(FastTests.class)
    public void categoryTest() {
        ...
    }    
}

With JUnit 5, the @Category annotation gets replaced with the more flexible @Tag annotation.

If you consider migrating to JUnit 5, this blog can be a good place to start. Also, this and this answer can show you how to filter the tests execution based on their tags, using Intellij IDEA or Maven.

Upvotes: 7

Related Questions