Both JUnit and TestNG use the @Test annotation to specify which method is considered to be a test. Unlike JUnit, which requires every method to be annotated with @Test, TestNG allows us to use this annotation on a class level, as well. When used in this way, all public methods are considered tests unless specified otherwise:
@Test public class DirectionSpec {
public void whenGetFromShortNameNThenReturnDirectionN() { Direction direction = Direction.getFromShortName('N'); assertEquals(direction, Direction.NORTH); }
public void whenGetFromShortNameWThenReturnDirectionW() { Direction direction = Direction.getFromShortName('W'); assertEquals(direction, Direction.WEST); } }
In this example, we put the @Test annotation above the DirectionSpec class. As a result, both the whenGetFromShortNameNThenReturnDirectionN and whenGetFromShortNameWThenReturnDirectionW methods are considered tests.
If that code was written using JUnit, both the methods would need to have the @Test annotation.