Annotations have made some of the programming stuffs easier because they do a lot of things in the background, which any programmer would otherwise needed to have coded by themselves. JUnit uses a bunch of annotations and represents various separate tasks they do behind the scene. Here are some of the most widely used annotations from Java based Unit Testing Framework – The JUnit. If you would like to see a comprehensive example of JUnit with all these annotations in action, see my previous article on JUnit Test Case
Denotes a test method. Can be used with expected to assert expected results on the object under test.
Run before each test method is run i.e. do a setup
Run after each test method is run i.e. do a teardown
Run before all the tests in a class (Runs once for all of the tests)
Run after all the tests in a class (Runs once for all of the tests)
Allows you to run the same test with different data by defining the data parameters. @Parameters have to return List, and the parameter will pass into class constructor as argument.
Exclusively tells the class that the Junit Test Case uses Parameterized runner
This allows to skip a test. You might want to skip a test if you are still working on it or you are not convinced it’s a valid test case or maybe ignore cases which are long running.
The following is a list of most of the valid @suppresswarning annotation parameters in Java for Eclipse and IBM RAD IDE. The right column shows what those java annotation parameters are used for.
interface non-inherited method compatibility
local variable hiding another variable
hidden catch block
to suppress warnings relative to non-nls string literals
assignment with no effect
to suppress warnings relative to null analysis
missing null check
missing @Override annotation
assignment to a parameter
attempt to override package-default method
usage a of raw type (instead of a parametrized type)
to suppress warnings relative to usage of discouraged or forbidden references
unnecessary semicolon or empty statement
to suppress warnings relative to missing serialVersionUID field for a serializable class
constructor or setter parameter hiding another field
to suppress warnings relative to incorrect static access
if a non static receiver is used to get a static field or call a static method
overriding a method without making a super invocation
to suppress warnings relative to unoptimized access from inner classes
enable support for tasks tags in source code
type parameter hiding another type
to suppress warnings relative to unchecked operations
unnecessary else clause
to suppress warnings relative to field access unqualified
to suppress warnings relative to unused code
unused method argument
unused import reference
unused local variable
unused private member declaration
unused declared thrown exception
unnecessary cast/instanceof operation
varargs argument need explicit cast
unhandled warning token in @SuppressWarnings
Here is an usage example. As you can see, the piece of code below does not use any annotations and the code has a yellow underline in the method signature. If you hover around the icon on the left bar of the IDE where there will be warning sign, you will notice that the Eclipse IDE has issued warning saying that this method is never called in the class.
But if you put the annotation @SuppressWarning(“unused”), we specifically tell the IDE to ignore the usused methods by suppressing the warnigns. Here is the code after the annotation has been added. Code looks clean, no more annoying warning underline !