When diving into the world of Java development, one of the essential tools you'll likely encounter is JUnit. As one of the premier testing frameworks for Java, JUnit helps developers ensure that their code runs as intended. Whether you're revising for an interview, a certification exam, or merely looking to gauge your knowledge, this set of multiple-choice questions will provide a comprehensive overview. Let's dive in!
1. What is JUnit primarily used for?
Answer:
Explanation:
JUnit is a testing framework primarily designed for unit testing in the Java programming language.
2. Which annotation is used to indicate a method as a test method in JUnit?
Answer:
Explanation:
The @Test annotation is used to mark a method as a test method in JUnit.
3. If a JUnit test method throws an exception, what will be the outcome?
Answer:
Explanation:
If a JUnit test method throws an exception, the test is considered a failure.
4. Which of the following is NOT a core feature of JUnit?
Answer:
Explanation:
While JUnit provides support for suites, runners, and listeners, it does not have an in-built profiler.
5. How do you specify that a particular test method should be ignored during execution?
Answer:
Explanation:
The @Ignore annotation is used to specify that a particular test should not be executed.
6. Which JUnit version introduced the use of annotations?
Answer:
Explanation:
JUnit 4 introduced the use of annotations to define test methods, setup methods, and more.
7. In which lifecycle method would you typically place setup code to run before every test method?
Answer:
Explanation:
The @BeforeEach annotation is used to denote a method that should be executed before each test method in the current class.
8. What does the @AfterEach annotation signify?
Answer:
Explanation:
The @AfterEach annotation is used to denote a method that should be executed after each test method in the current class.
9. Which JUnit annotation allows parameterized tests?
Answer:
Explanation:
The @ParameterizedTest annotation in JUnit allows for parameterized tests.
10. How can you expect a specific exception to be thrown in a JUnit test?
Answer:
Explanation:
The assertThrows() method is used in JUnit to assert that a specific exception type is thrown.
11. How do you specify a timeout for a test method in JUnit?
Answer:
Explanation:
In JUnit 4, you can specify a timeout for a test method using the timeout attribute in the @Test annotation.
12. In which version of JUnit was the Jupiter API introduced?
Answer:
Explanation:
The Jupiter API, which provides a new programming and extension model for writing tests, was introduced in JUnit 5.
13. Which of the following assertions checks that a condition is true?
Answer:
Explanation:
The assertTrue() method is used to assert that a condition is true.
14. What is the purpose of the @DisplayName annotation in JUnit?
Answer:
Explanation:
The @DisplayName annotation in JUnit allows you to define a custom display name for the test method or test class.
15. How can you group multiple test classes together in JUnit?
Answer:
Explanation:
In JUnit, the @Suite annotation is used to group multiple test classes together.
16. Which method is used in JUnit to check if two objects are the same instance?
Answer:
Explanation:
The assertSame() method checks if two objects are the same instance.
17. How do you specify that a method should be executed only once before any test methods in a test class?
Answer:
Explanation:
The @BeforeAll annotation is used to denote a method that should be executed once before any of the test methods in the test class.
18. Which version of JUnit introduced lambda functions for assertions?
Answer:
Explanation:
JUnit 5 introduced the ability to use lambda functions in assertions, enhancing the way we can write tests.
19. Which assertion method is used to check if an object is null in JUnit?
Answer:
Explanation:
The assertNull() method is used to assert that an object is null.
20. Which of the following is NOT a type of assertion provided by JUnit?
Answer:
Explanation:
While JUnit provides assertions for timeouts, conditions, and exceptions, it does not provide repetition assertions.
Comments
Post a Comment
Leave Comment