What is JUnit?
What are annotations in JUnit?
@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.
What are test suites in JUnit?
@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.
What is the @Ignore annotation in JUnit?
@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?
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
@ExtendWithfor 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
Vintageengine.
No comments:
Post a Comment