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.
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.
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.
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
@Test
@Before
@After
@AfterClass
@BeforeClass
@Ignore
@Runwith
For Parameterized tests use @Parameters
and @RunWith(Parameterized.class)
https://github.com/junit-team/junit/wiki/Parameterized-tests
@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
@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
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With