Junit FAQ's

JUnit is a unit testing framework for Java. It helps us write and run test cases to check if our code works correctly. Mainly used to test small parts like methods or classes.

  • @Test – Marks a method as a test case.
  • @Before – Runs before every test case.
  • @After – Runs after every test case.
  • @BeforeClass – Runs once before all test cases (must be static).
  • @AfterClass – Runs once after all test cases (must be static).
  • @Ignore – Skips the test case from execution.

JUnit Annotation Execution Order:

  • @BeforeClass
  • @Before
  • @Test
  • @After
  • @AfterClass
  • assertEquals(expected, actual) – Passes if both values are equal.
  • assertTrue(condition) – Passes if condition is true.
  • assertFalse(condition) – Passes if condition is false.
  • assertNull(object) – Passes if object is null.
  • assertNotNull(object) – Passes if object is not null.
  • fail() – Fails the test immediately when called.

@Ignore is used to skip a test method temporarily during test execution without deleting it.

In JUnit, we use the @RunWith(Suite.class) and @Suite.SuiteClasses({}) annotations to run multiple test classes together as a test suite. It allows grouping of test classes and executing them as one unit.