What is JUnit?
JUnit is a widely used testing framework for Java applications. It provides annotations, assertions, and test runners to simplify the process of writing and running unit tests.
What are annotations in JUnit?
Annotations in JUnit define the lifecycle of a test. Some common annotations are:
@Test
: Marks a method as a test method@Before
: Runs before each test method (JUnit 4).@After
: Runs after each test method (JUnit 4).@BeforeClass
: Runs once before all tests (JUnit 4).@AfterClass
: Runs once after all tests (JUnit 4).@Ignore
: Ignores a test method (JUnit 4).@Test(expected = Exception.class)
: Tests that the specified exception is thrown (JUnit 4).
@Before
and @BeforeClass
?@Before
: Runs before each individual test method.@BeforeClass
: Runs once before all the tests in the test class. It’s a static method.
What is the difference between @After
and @AfterClass?
@AfterClass
: Runs once after all tests have been executed. It’s a static method.@After
: Runs after each individual test method.
A test suite is a collection of test cases that are run together. You can use the @RunWith
and @Suite
annotations to define a test suite, allowing you to group multiple tests together to run them sequentially.
What is the purpose of assert
methods in JUnit?
assertTrue(condition)
: Checks if the condition is true.assertFalse(condition)
: Checks if the condition is false.assertEquals(expected, actual)
: Checks if the expected value matches the actual value.assertNotNull(object)
: Checks if the object is not null.assertNull(object)
: Checks if the object is null.
@Ignore
annotation in JUnit?The
@Ignore
annotation is used to temporarily disable a test method or a test class from being executed. This is useful when a test is not relevant or is under development.What is the purpose of the @Test(expected = Exception.class)
annotation in JUnit?
This annotation is used to specify that the test should pass only if the given exception is thrown during the execution of the test method.
How do you handle timeouts in JUnit tests?
You can use the@Test(timeout = 1000)
annotation to specify that a test should fail if it takes longer than the given timeout (in milliseconds).What is the difference between JUnit 4 and JUnit 5?
- Architecture: JUnit 5 is modular (JUnit Platform, Jupiter, Vintage), while JUnit 4 is monolithic.
-
Annotations: JUnit 5 introduces
@BeforeEach
,@AfterEach
,@BeforeAll
,@AfterAll
(replacing JUnit 4's@Before
,@After
,@BeforeClass
,@AfterClass
). -
Test Lifecycle: JUnit 5 allows non-static lifecycle methods (
@BeforeAll
,@AfterAll
), whereas JUnit 4 requires static methods. -
Test Execution Order: JUnit 5 supports customizable execution order with
@TestMethodOrder
, while JUnit 4 runs tests in code order. -
Extensions: JUnit 5 uses
@ExtendWith
for extensions, replacing JUnit 4’s@Rule
/@ClassRule
. -
Parameterization: JUnit 5 provides easier parameterized tests with
@ParameterizedTest
, compared to JUnit 4's@RunWith(Parameterized.class)
. -
Exception Handling: JUnit 5 uses
assertThrows()
for exception assertions, while JUnit 4 uses@Test(expected = ...)
. -
Parallel Execution: JUnit 5 supports parallel test execution natively, unlike JUnit 4.
-
Backward Compatibility: JUnit 5 supports JUnit 4 tests via the
Vintage
engine.