Share
Explore

Using JUNIT 5

Think of annotations as =[Comments, Program Execution Directives]
Annotations are used to provide supplemental information about a program.
Annotations start with ‘@’.
Annotations do not change the action of a compiled program.
Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.
Annotations are not pure comments as they can change the way a program is treated by the compiler.
Hierarchy of Annotations in Java
Implementation:
Note: This program throws a compiler error because we have mentioned override, but not overridden, we have overloaded display.

Java
// Java Program to Demonstrate that Annotations
// are Not Barely Comments
// Class 1
class Base {
// Method
public void display()
{
System.out.println("Base display()");
}
}
// Class 2
// Main class
class Derived extends Base {
// Overriding method as already up in above class
@Override public void display(int x)
{
// Print statement when this method is called
System.out.println("Derived display(int )");
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Creating object of this class inside main()
Derived obj = new Derived();
// Calling display() method inside main()
obj.display();
}
}
Output:
10: error: method does not override or implement
a method from a supertype
If we remove parameter (int x) or we remove @override, the program compiles fine.
Categories of Annotations
There are broadly 5 categories of annotations as listed:
Marker Annotations
Single value Annotations
Full Annotations
Type Annotations
Repeating Annotations

Category 1: Marker Annotations
Used to mark a declaration. These annotations contain no members and do not consist of any data. Thus, its presence as an annotation is sufficient. Since the marker interface contains no members, simply determining whether it is present or absent is sufficient. @Override is an example of Marker Annotation.
Example
@TestAnnotation()
Category 2: Single value Annotations
These annotations contain only one member and allow a shorthand form of specifying the value of the member.

We only need to specify the value for that member when the annotation is applied and don’t need to specify the name of the member. However, in order to use this shorthand, the name of the member must be a value.
Example

@TestAnnotation(“testing”);

Category 3: Full Annotations
These annotations consist of multiple data members, names, values, pairs.
Example
@TestAnnotation(name=”Peanut”, breed=”Bichon”)

Category 4: Type Annotations
These annotations can be applied to any place where a type is being used. For example, we can annotate the return type of a method. These are declared annotated with @Target annotation.
Example
Java
// Java Program to Demonstrate Type Annotation
// Importing required classes
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
// Using target annotation to annotate a type
@Target(ElementType.TYPE_USE)
// Declaring a simple type annotation
@interface TypeAnnoDemo{}
// Main class
public class GFG {
// Main driver method
public static void main(String[] args) {
// Annotating the type of a string
@TypeAnnoDemo String string = "I am annotated with a type annotation";
System.out.println(string);
abc();
}
// Annotating return type of a function
static @TypeAnnoDemo int abc() {
System.out.println("This function's return type is annotated");
return 0;
}
}
Output:
I am annotated with a type annotation
This function's return type is annotated
Category 5: Repeating Annotations
These are the annotations that can be applied to a single item more than once. For an annotation to be repeatable it must be annotated with the @Repeatable annotation, which is defined in the java.lang.annotation package. Its value field specifies the container type for the repeatable annotation. The container is specified as an annotation whose value field is an array of the repeatable annotation type.

To create a repeatable annotation, first: the container annotation is created, and then the annotation type is specified as an argument to the @Repeatable annotation.
Example:
Java
// Java Program to Demonstrate a Repeatable Annotation
// Importing required classes
import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// Make Words annotation repeatable
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyRepeatedAnnos.class)
@interface Words
{
String word() default "Hello";
int value() default 0;
}
// Create container annotation
@Retention(RetentionPolicy.RUNTIME)
@interface MyRepeatedAnnos
{
Words[] value();
}
public class Main {
// Repeat Words on newMethod
@Words(word = "First", value = 1)
@Words(word = "Second", value = 2)
public static void newMethod()
{
Main obj = new Main();
try {
Class<?> c = obj.getClass();
// Obtain the annotation for newMethod
Method m = c.getMethod("newMethod");
// Display the repeated annotation
Annotation anno
= m.getAnnotation(MyRepeatedAnnos.class);
System.out.println(anno);
}
catch (NoSuchMethodException e) {
System.out.println(e);
}
}
public static void main(String[] args) { newMethod(); }
}
Output:
@MyRepeatedAnnos(value={@Words(value=1, word="First"), @Words(value=2, word="Second")})
Predefined/ Standard Annotations
Java popularly defines seven built-in annotations as we have seen up in the hierarchy diagram.
Four are imported from java.lang.annotation: @Retention, @Documented, @Target, and @Inherited.
Three are included in java.lang: @Deprecated, @Override and @SuppressWarnings
Annotation 1: @Deprecated
It is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form.
The Javadoc should be used when an element has been deprecated.
@deprecated tag is for documentation and @Deprecated annotation is for runtime reflection.
@deprecated tag has higher priority than @Deprecated annotation when both are together used.
Example:

public class DeprecatedTest
{
@Deprecated
public void Display()
{
System.out.println("Deprecatedtest display()");
}
public static void main(String args[])
{
DeprecatedTest d1 = new DeprecatedTest();
d1.Display();
}
}
Output
Deprecatedtest display()
Annotation 2: @Override
It is a marker annotation that can be used only on methods. A method annotated with @Override must override a method from a superclass. If it doesn’t, a compile-time error will result (see
for example). It is used to ensure that a superclass method is actually overridden, and not simply overloaded.
Example

// Java Program to Illustrate Override Annotation
// Class 1
class Base
{
public void Display()
{
System.out.println("Base display()");
}
public static void main(String args[])
{
Base t1 = new Derived();
t1.Display();
}
}
// Class 2
// Extending above class
class Derived extends Base
{
@Override
public void Display()
{
System.out.println("Derived display()");
}
}
Output
Derived display()
Annotation 3: @SuppressWarnings
It is used to inform the compiler to suppress specified compiler warnings. The warnings to suppress are specified by name, in string form. This type of annotation can be applied to any type of declaration.
Java groups warnings under two categories. They are deprecated and unchecked. Any unchecked warning is generated when a legacy code interfaces with a code that uses generics.
Example:

// Java Program to illustrate SuppressWarnings Annotation
// Class 1
class DeprecatedTest
{
@Deprecated
public void Display()
{
System.out.println("Deprecatedtest display()");
}
}
// Class 2
public class SuppressWarningTest
{
// If we comment below annotation, program generates
// warning
@SuppressWarnings({"checked", "deprecation"})
public static void main(String args[])
{
DeprecatedTest d1 = new DeprecatedTest();
d1.Display();
}
}
Output
Deprecatedtest display()
Annotation 4: @Documented
It is a marker interface that tells a tool that an annotation is to be documented. Annotations are not included in ‘Javadoc’ comments. The use of @Documented annotation in the code enables tools like Javadoc to process it and include the annotation type information in the generated document.
Annotation 5: @Target
It is designed to be used only as an annotation to another annotation. @Target takes one argument, which must be constant from the ElementType enumeration. This argument specifies the type of declarations to which the annotation can be applied. The constants are shown below along with the type of declaration to which they correspond.

Table 1
0
Search
Target Constant
Annotations Can Be Applied To
1
ANNOTATION_TYPE
Another annotation
2
CONSTRUCTOR
Constructor
3
FIELD
Field
4
LOCAL_VARIABLE
Local variable
5
METHOD
Method
6
PACKAGE
Package
7
PARAMETER
Parameter
8
TYPE
Class, Interface, or enumeration
There are no rows in this table

We can specify one or more of these values in a @Target annotation.

To specify multiple values, we must specify them within a braces-delimited list.

For example, to specify that an annotation applies only to fields and local variables, you can use this @Target annotation: @Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE}) @Retention Annotation It determines where and how long the annotation is retent.

The 3 values that the @Retention annotation can have:
SOURCE: Annotations will be retained at the source level and ignored by the compiler.
CLASS: Annotations will be retained at compile-time and ignored by the JVM.
RUNTIME: These will be retained at runtime.

Annotation 6: @Inherited
@Inherited is a marker annotation that can be used only on annotation declaration. It affects only annotations that will be used on class declarations. @Inherited causes the annotation for a superclass to be inherited by a subclass. Therefore, when a request for a specific annotation is made to the subclass, if that annotation is not present in the subclass, then its superclass is checked. If that annotation is present in the superclass, and if it is annotated with @Inherited, then that annotation will be returned.
Annotation 7: User-defined (Custom)
User-defined annotations can be used to annotate program elements, i.e. variables, constructors, methods, etc. These annotations can be applied just before the declaration of an element (constructor, method, classes, etc).
Syntax: Declaration
[Access Specifier] @interface<AnnotationName>
{
DataType <Method Name>() [default value];
}
Do keep these certain points as rules for custom annotations before implementing user-defined annotations.
AnnotationName is an identifier.
The parameter should not be associated with method declarations and throws clause should not be used with method declaration.
Parameters will not have a null value but can have a default value.
default value is optional.
The return type of method should be either primitive, enum, string, class name, or array of primitive, enum, string, or class name type.
Example:
Java
// Java Program to Demonstrate User-defined Annotations
package source;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
// User-defined annotation
@Documented
@Retention(RetentionPolicy.RUNTIME)
@ interface TestAnnotation
{
String Developer() default "Rahul";
String Expirydate();
} // will be retained at runtime
// Driver class that uses @TestAnnotation
public class Test
{
@TestAnnotation(Developer="Rahul", Expirydate="01-10-2020")
void fun1()
{
System.out.println("Test method 1");
}
@TestAnnotation(Developer="Anil", Expirydate="01-10-2021")
void fun2()
{
System.out.println("Test method 2");
}
public static void main(String args[])
{
System.out.println("Hello");
}
}
Output:
Hello


What is JUnit Annotations?
JUnit Annotations is a special form of syntactic meta-data that can be added to Java source code for better code readability and structure.

Variables, parameters, packages, methods and classes can be annotated.

Annotations were introduced in Junit4, which makes Java code more readable and simple. This is the big difference between Junit3 and Junit4 that Junit4 is annotation based.

With the knowledge of annotations in Junit5, one can easily learn and implement a JUnit test. Below is the important and frequently used JUnit annotations list:

Table 2
0
Search
S.No.
Annotations
Description
1
1
@Test
This annotation is a replacement of org.junit.TestCase which indicates that public void method to which it is attached can be executed as a test Case.
2
2
@Before
This annotation is used if you want to execute some statement such as preconditions before each test case.
3
3
@BeforeClass
This annotation is used if you want to execute some statements before all the test cases for e.g. test connection must be executed before all the test cases.
4
4
@After
This annotation can be used if you want to execute some statements after each Test Case for e.g resetting variables, deleting temporary files ,variables, etc.
5
5
@AfterClass
This annotation can be used if you want to execute some statements after all test cases for e.g. Releasing resources after executing all test cases.
6
6
@Ignores
This annotation can be used if you want to ignore some statements during test execution for e.g. disabling some test cases during test execution.
7
7
@Test(timeout=500)
This annotation can be used if you want to set some timeout during test execution for e.g. if you are working under some SLA (Service level agreement), and tests need to be completed within some specified time.
8
8
@Test(expected=IllegalArgumentException.class)
This annotation can be used if you want to handle some exception during test execution. For, e.g., if you want to check whether a particular method is throwing specified exception or not.
There are no rows in this table
JUnit Test Cases Class
To run multiple test, TestCase class is available in org.junit.TestCase packages.

@Test annotation tells JUnit that this public void method (Test Case here) to which it is attached can be run as a test case.

Methods available in org.junit.TestCase class
0
Search
S.No.
Method
Description
1
1
int countTestCases()
This method is used to count how many number of test cases executed by run(TestResult tr) method.
2
2
TestResult createResult()
This method is used to create a TestResult object.
3
3
String getName()
This method returns a string which is nothing but a TestCase.
4
4
TestResult run()
This method is used to execute a test which returns a TestResult object
5
5
void run(TestResult result)
This method is used to execute a test having a TestResult object which doesn’t returns anything.
6
6
void setName(String name)
This method is used to set a name of a TestCase.
7
7
void setUp()
This method is used to write resource association code. e.g. Create a database connection.
8
8
void tearDown()
This method is used to write resource release code. e.g. Release database connection after performing transaction operation.
There are no rows in this table
JUnit TestResult Class
When you execute a test, it returns a result (in the form of TestResult object). This TestResult object can be used to analyse the resultant object. This test result can be either failure or successful.
See below table for important methods used in org.junit.TestResult class:
Table 4
0
Search
S.No.
Method
Description
1
1
void addError(Test test, Throwable t)
This method is used if you require add an error to the test.
2
2
void addFailure(Test test, AssertionFailedError t)
This method is used if you require add a failure to the list of failures.
3
3
void endTest(Test test)
This method is used to notify that a test is performed(completed)
4
4
int errorCount()
This method is used to get the error detected during test execution.
5
5
Enumeration<TestFailure> errors()
This method simply returns a collection (Enumeration here) of errors.
6
6
int failureCount()
This method is used to get the count of errors detected during test execution.
7
7
void run(TestCase test)
This method is used to execute a test case.
8
8
int runCount()
This method simply counts the executed test.
9
9
void startTest(Test test)
This method is used to notify that a test is started.
10
10
void stop()
This method is used to test run to be stopped.
There are no rows in this table

JUnit Test Suite Class
If you want to execute multiple tests in a specified order, it can be done by combining all the tests in one place. This place is called as the test suites.
See below table for important methods used in org.junit.TestSuite class:
Table 5
0
Search
S.No.
Method
Description
1
1
void addTest(Test test)
This method is used if you want to add a test to the suite.
2
2
void addTestSuite(Class<? extends TestCase> testClass)
This method is used if you want to specify the class while adding a test to the suite.
3
3
int countTestCases()
This method is used if you want to count the number of test cases.
4
4
String getName()
This method is used to get the name of the test suite.
5
5
void run(TestResult result)
This method is used to execute a test and collect test result in TestResult object.
6
6
void setName(String name)
This method is used to set the name of TestSuite.
7
7
Test testAt(int index)
This method is used if you want to return the test at given index.
8
8
int testCount()
This method is used if you want to return a number of tests in the Suite.
9
9
static Test warning(String message)
This method returns a test which will fail and log a warning message.
There are no rows in this table
Summary:
JUnit provides a portable API, which provides all important classes and Selenium annotations useful in writing a unit test.
Classes which are very useful while writing a test case
org.junit.Assert
org.junit.TestCase
org.junit.TestResult
org.junit.TestSuite
Important and frequently used JUnit annotations list
@Before@BeforeClass
@After
@AfterClass
@Test
@Ignore

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.