Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of annotations in JUnit

Tags:

java

junit4

Recently, I have studied and implemented the JUnit framework. As a result i am aware of few annotations which are used in JUnit :- @Test, @Before, @After, @Ignore, @BeforeClass, @AfterClass, @Runwith(Suite.class), @SuiteClasses({}), @Parameters, @RunWith(Parameterized.class) and @Rule.

I am sure there are more annotations which are used in JUnit. Can anybody guide me with a list of more annotations that can be used and under what circumstances they are used?

Thanks.

like image 974
silver_noodles Avatar asked Apr 02 '13 09:04

silver_noodles


People also ask

What is @test annotation in JUnit?

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure.

Which of the annotation is not available in JUnit?

Explanation: Auto, Table, Identity and Sequence are the ID generating strategies using @GeneratedValue annotation. 7. Which one of the following is not an annotation used by Junit with Junit4? Explanation: @Test, @Before, @BeforeClass, @After, @AfterClass and @Ignores are the annotations used by Junit with Junit4.


1 Answers

This Github search (@interface) gives you the list of all the annotations :

https://github.com/junit-team/junit/search?q=%22%40interface%22&type=Code

Basic Annotations

@Test @Before @After @AfterClass @BeforeClass @Ignore @Runwith

Parameterized Tests

For Parameterized tests use @Parameters and @RunWith(Parameterized.class)
https://github.com/junit-team/junit/wiki/Parameterized-tests

Category

@Category
Grouping tests into categories. e.g. Fast, Slow etc.

https://github.com/junit-team/junit/wiki/Categories

@IncludeCategory
Runs only the classes and methods that are annotated with either the category given with the @IncludeCategory annotation, or a subtype of that category.

@ExcludeCategory
Inverse of @IncludeCategory

Rules

@Rule
Rules allow very flexible addition or redefinition of the behavior of each test method in a test class. e.g. Creating a Temp Folder rule for creating a temp folder while running tests.

https://github.com/junit-team/junit/wiki/Rules

Theory and related annotations

@Theory
Theories give more flexible and expressive assertions

https://github.com/junit-team/junit/wiki/Theories

@DataPoint
Annotating an field or method with @DataPoint will cause the field value or the value returned by the method to be used as a potential parameter for theories in that class

@DataPoints

Extension of @Datapoint
Annotating an array or iterable-typed field or method with @DataPoints will cause the values in the array or iterable given to be used as potential parameters for theories in that class

@FromDataPoints

Annotating a parameter of a @Theory method with @FromDataPoints will limit the datapoints considered as potential values for that parameter to just the @DataPoints with the given name

@ParametersSuppliedBy
Annotating a @Theory method parameter with @ParametersSuppliedBy causes it to be supplied with values from the named ParameterSupplier when run as a theory

@TestedOn

The @TestedOn annotation takes an array of values to be used as data points for the annotated parameter.

e.g.

@Theory public void multiplyIsInverseOfDivideWithInlineDataPoints(         @TestedOn(ints = {0, 5, 10}) int amount,         @TestedOn(ints = {0, 1, 2}) int m ) {     assumeThat(m, not(0));     assertThat(new Dollar(amount).times(m).divideBy(m).getAmount(), is(amount)); } 
like image 57
Ajay George Avatar answered Sep 22 '22 00:09

Ajay George