In this session, I have discussed on what are Annotations and why Annotations are used and how to use Annotations.
For more updates and videos please follow
/ 322389966273813
Annotations is a feature of Java
Annotations denote meta data to Java Source code
Predefined Annotations are available in Java
User can define own Annotations
“@” symbol is used before the annotation name
Annotations helps to organize tests in simplified manner.
JUNIT 4 Annotations
@Before
@BeforeClass
@After
@AfterClass
@Test
@Ignore
@Test(timeout=500)
@Test(expected=IllegalArgumentException.class)
JUNIT 5 Annotations
@Test
@ParameterizedTest
@RepeatedTest
@TestFactory
@TestTemplate
@TestMethodOrder
@TestInstance
@DisplayName
@DisplayNameGeneration
@BeforeEach
@AfterEach
@BeforeAll
@AfterAll
@Nested
@Tag
@Disabled
@Timeout
@ExtendWith
@RegisterExtension
@TempDir
JUnit 4 Annotations : Details
================================
JUNIT 4 Annotations
@BeforeClass – Executed once before all the tests ( Note : Should be Static Method )
@Before – Executed before each test JUnit Test Method ( @Test)
@Test – Test Method which contains your test case
@After – Executed after each test JUnit Test Method (@Test)
@AfterClass – Executed once after all the tests ( Note : Should be Static Method )
@Ignore – To Ignore any test case
@RunWith – Invokes the class that contains JUnit tests
@Test (timeout=500) – To Timeout a test case after certain period
@Test(expected=IllegalArgumentException.Class) – Checks if test case is generating a particular exception
JUnit 5 Annotations Details
==============================
@Test – Test Method which contains test case
@BeforeEach – Executed before each JUnit Test Method ( @Test , @RepeatedTest,@ParameterizedTest,@TestFactory)
@After Each– Executed before each JUnit Test Method ( @Test , @RepeatedTest,@ParameterizedTest,@TestFactory)
@BeforeAll – Executed before all JUnit Test Methods ( @Test , @RepeatedTest,@ParameterizedTest,@TestFactory)
@AfterAll – Executed before all JUnit Test Methods ( @Test , @RepeatedTest,@ParameterizedTest,@TestFactory)
@ParameterizedTest – Test Method is parameterized
@RunWith – A Test Template or Repeated Test
@TestFactory – Used for Dynamic Tests
@TestMethodOrder – Configures Test Method execution order
@DisplayName – Custom display name for test cases
@Tag – Used for filtering tests, either in Method level or class level
@Disabled – To disable Test Method
@Timeout – Specifies a time duration to wait until failure
@Tested – Class inside a Class to handle Complex test case structures
================================
Example Program for practise
================================
package junit4Annotations;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class JUNIT_4_Annotations_Examples
{
@BeforeClass public static void BefCl() {
System.out.println("Connect to DB "); }
@Before
public void Bef()
{
System.out.println("Refresh the page");
}
@Test
public void Test1()
{
System.out.println("This is test case 1 ");
}
@Ignore("not yet implemented")
@Test
public void Test2()
{
System.out.println("This is test case 2 ");
}
@After
public void Aft()
{
System.out.println("Refresh the page ");
}
@AfterClass public static void AftCl() {
System.out.println("Disconnect DB"); }
@Test(expected=ArithmeticException.class)
public void test3ExceptionTest()
{
int a=1/0;
}
@Test(timeout=500)
public void test4timeout()
{
int a=0;
}
}