Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark one test class with many categories

Tags:

java

junit

I have to mark one test with multiple test categories using JUnit 4.11 annotation @Category. Is it possible? If yes, please, give a little example code.

like image 701
shprotova Avatar asked Feb 21 '13 13:02

shprotova


People also ask

Which annotations are used for creating test categories?

org.junit.experimental.categories From a given set of test classes, runs only the classes and methods that are annotated with either the category given with the @IncludeCategory annotation, or a subtype of that category. Note that, for now, annotating suites with @Category has no effect.

Which methods are used in test class to initialize and release common objects?

You declare these objects as a private variable, and initialize them by overriding the setUp() or via the constructor. You can perform clean-up operations by overriding tearDown() . Each test method runs on its own TestCase instance with its own set of text fixtures.

What is the use of @TAG in JUnit?

@Tag is a repeatable annotation that is used to declare a tag for the annotated test class or test method. Tags are used to filter which tests are executed for a given test plan. For example, a development team may tag tests with values such as "fast" , "slow" , "ci-server" , etc.


2 Answers

Pass an array of categories, as shown in the javadoc.

@Category({SlowTests.class, FastTests.class})
public static class B {
    @Test
    public void c() {
    }
}
like image 122
JB Nizet Avatar answered Oct 04 '22 16:10

JB Nizet


public static class B {
       @Test
       @Category( { SlowTests.class, FastTests.class })
       public void c() {
       }
}

This is taken from the javadoc for Categories.

like image 38
nattyddubbs Avatar answered Oct 04 '22 16:10

nattyddubbs