Debug School

Akanksha
Akanksha

Posted on

Top 30 Junit Interview Questions with Answers multiple choice style

1. What is JUnit?

a) A Java build tool
b) A Java testing framework
c) A Java IDE
d) A Java database library
Answer: b) A Java testing framework

2. Which annotation is used to mark a method as a test method in JUnit?

a) @Test
b) @TestMethod
c) @Unit
d) @TestCase
Answer: a) @Test

3. What is the purpose of the @Before annotation in JUnit?

a) It marks a method as a test setup method.
b) It marks a method as an ignored test.
c) It marks a method as a test teardown method.
d) It marks a method as a parameterized test.
Answer: a) It marks a method as a test setup method.

4. In JUnit, which method is used to assert that two values are equal?

a) assertEqual()
b) assertEquals()
c) assertSame()
d) assertEqualsTo()
Answer: b) assertEquals()

5. What does the @Ignore annotation do in JUnit?

a) It marks a test method as ignored.
b) It marks a test method as a parameterized test.
c) It specifies the time duration for test execution.
d) It defines a custom assertion.
Answer: a) It marks a test method as ignored.

6. Which JUnit rule is used to test expected exceptions?

a) @Rule
b) @ExpectedException
c) @TestRule
d) @RuleException
Answer: b) @ExpectedException

7. In JUnit, which method is used to run setup code before running each test method?

a) @BeforeClass
b) @BeforeTest
c) @Before
d) @BeforeMethod
Answer: c) @Before

8. Which JUnit annotation is used to run a test method repeatedly with different inputs?

a) @Repeat
b) @ParameterizedTest
c) @RunTest
d) @RepeatedTest
Answer: d) @RepeatedTest

9. What is the purpose of the @After annotation in JUnit?

a) It marks a method as a test setup method.
b) It marks a method as an ignored test.
c) It marks a method as a test teardown method.
d) It marks a method as a parameterized test.
Answer: c) It marks a method as a test teardown method.

10. Which JUnit assertion method is used to check if a value is not null?

a) assertNotEquals()
b) assertNotNull()
c) assertNonNull()
d) assertNotNull()
Answer: d) assertNotNull()

11. In JUnit 5, what is the default test engine?

a) JUnit3
b) JUnit4
c) JUnit Jupiter
d) TestNG
Answer: c) JUnit Jupiter

12. What is the purpose of JUnit's @RunWith annotation?

a) It specifies the order of test execution.
b) It marks a test class as abstract.
c) It indicates the test runner to use.
d) It defines custom test rules.
Answer: c) It indicates the test runner to use.

13. Which JUnit 5 extension is used to conditionally enable or disable tests?

a) @TestEnabled
b) @EnabledOn
c) @DisabledOn
d) @EnabledIf
Answer: b) @EnabledOn

14. What is the primary goal of unit testing?

a) To test the entire application's functionality
b) To test the integration between different components
c) To test individual units or components in isolation
d) To test user interfaces and user experiences
Answer: c) To test individual units or components in isolation

15. In JUnit, which method is used to perform cleanup operations after each test method?

a) @AfterTest
b) @After
c) @AfterClass
d) @AfterMethod
Answer: b) @After

16. Which JUnit assertion method is used to check if two arrays are equal?

a) assertArrayEquals()
b) assertEqualsArray()
c) arrayEquals()
d) checkArrays()
Answer: a) assertArrayEquals()

17. In JUnit, what is the purpose of the @RunWith annotation with the Parameterized class?

a) It runs parameterized tests.
b) It defines custom rules.
c) It marks a test class as abstract.
d) It sets the test timeout.
Answer: a) It runs parameterized tests.

18. What does the @DisplayName annotation do in JUnit 5?

a) It specifies the author of the test.
b) It sets the display name for a test method.
c) It defines a custom test rule.
d) It marks a test class as a parameterized test.
Answer: b) It sets the display name for a test method.

19. Which JUnit 5 annotation is used to disable a test method?

a) @Ignore
b) @Disabled
c) @Exclude
d) @Deactivate
Answer: b) @Disabled

20. What is the purpose of JUnit's @RunWith annotation with the Suite class?

a) It defines a custom test suite.
b) It marks a test class as abstract.
c) It specifies the test order.
d) It sets the test timeout.
Answer: a) It defines a custom test suite.

21. In JUnit 5, which annotation is used to specify the maximum time a test method should take to complete?

a) @Timeout
b) @MaxTime
c) @ExecutionTime
d) @TestTimeout
Answer: a) @Timeout

22. What is the purpose of JUnit's @Tag annotation in JUnit 5?

a) It marks a test method as ignored.
b) It specifies the test order.
c) It categorizes tests for selective execution.
d) It defines custom test rules.
Answer: c) It categorizes tests for selective execution.

23. Which JUnit assertion method is used to check if a condition is true?

a) assertTrue()
b) assertCondition()
c) checkTrue()
d) assertIsTrue()
Answer: a) assertTrue()

24. In JUnit, which annotation is used to specify the order of test methods?

a) @Order
b) @TestOrder
c) @TestMethodOrder
d) @MethodOrder
Answer: c) @TestMethodOrder

25. What is the purpose of JUnit's @BeforeEach annotation in JUnit 5?

a) It marks a method as a test setup method.
b) It marks a test method as ignored.
c) It marks a method as a test teardown method.
d) It runs the test class constructor.
Answer: a) It marks a method as a test setup method.

26. In JUnit, which assertion method is used to compare two doubles with a delta value?

a) assertDoubleEquals()
b) assertDeltaEquals()
c) assertEqualsDelta()
d) assertEqualsWithDelta()
Answer: c) assertEqualsDelta()

27. What is the purpose of JUnit's @AfterEach annotation in JUnit 5?

a) It marks a method as a test setup method.
b) It marks a test method as ignored.
c) It marks a method as a test teardown method.
d) It specifies the test order.
Answer: c) It marks a method as a test teardown method.

28. In JUnit 5, which annotation is used to specify the source of test data for parameterized tests?

a) @DataProvider
b) @ValueSource
c) @ParameterSource
d) @MethodSource
Answer: d) @MethodSource

29. What is the purpose of JUnit's @AfterAll annotation in JUnit 5?

a) It marks a method as a test setup method.
b) It marks a test method as ignored.
c) It marks a method as a test teardown method.
d) It performs cleanup operations after all test methods in the class.
Answer: d) It performs cleanup operations after all test methods in the class.

30. In JUnit, which assertion method is used to check if two objects refer to the same object?

a) assertObjectEquals()
b) assertSame()
c) assertEquals()
d) assertReferenceEquals()
Answer: b) assertSame()

Top comments (0)