Last Updated On
Welcome to the JUnit Quiz! This blog post features 25 multiple-choice questions that explore concepts of JUnit.
1. What is JUnit primarily used for?
Select the best answer
a) Debugging
b) Profiling
c) Unit Testing
d) Continuous Integration
Answer 1
c) Unit Testing
JUnit is a testing framework primarily designed for unit testing in the Java programming language.
2. Which of the following is not correct about JUnit?
Choose one option
a) Junit provides test runners for running test
b) Junit cannot be run automatically
c) Junit is the open-source framework
d) Junit provides an annotation to identify test methods
Answer 2
b) Junit cannot be run automatically
JUnit test can be run automatically and they check their own results and provide immediate feedback.
3. Which annotation is used to indicate a method as a test method in JUnit?
Choose one option
a) @Testify
b) @TestCase
c) @Testing
d) @Test
Answer 3
d) @Test
The @Test annotation is used to mark a method as a test method in JUnit.
4. JUnit test files are written in files with which file extension?
Choose one option
a) .unit
b) .java
c) .test
d) .junit
Answer 4
b) .java
JUnit test files are regular java files with special methods which are referenced via annotations.
5. Which of the following is NOT a core feature of JUnit?
Choose one option
a) Test suites
b) Test runners
c) Test profilers
d) Test listeners
Answer 5
c) Test profilers
While JUnit provides support for suites, runners, and listeners, it does not have an in-built profiler.
6. If a JUnit test method throws an exception, what will be the outcome?
a) Success
b) Compilation error
c) Failure
d) Ignored
Answer 6
c) Failure
If a JUnit test method throws an exception, the test is considered a failure.
7. How do you specify that a particular test method should be ignored during execution?
a) @IgnoreTest
b) @Skip
c) @Ignore
d) @Avoid
Answer 7
c) @Ignore
In JUnit, you can specify that a test method should be ignored during execution by using the `@Ignore` annotation. This annotation tells the JUnit framework to skip the execution of the annotated test method.
8. Which JUnit version introduced the use of annotations?
Choose one option
a) JUnit 3
b) JUnit 4
c) JUnit 5
d) JUnit 2
Answer 8
b) JUnit 4
9. Which of the following annotation causes that method run once before any of the test methods in the class?
Choose one option
a) @Test
b) @Before
c) @BeforeClass
d) @AfterClass
Answer 9
c) @BeforeClass
Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class.
10. The @Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.
Choose one option
a) True
b) False
Answer 10
a) True
The @Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.
11. What does the @AfterEach annotation signify?
Choose one option
a) It runs after all test methods in the current test class.
b) It runs before each test method.
c) It runs after each test method.
d) It runs before all test methods in the current test class.
Answer 11
c) It runs after each test method.
The @AfterEach annotation is used to denote a method that should be executed after each test method in the current class.
12. Which JUnit annotation allows parameterized tests?
Choose one option
a) @Parameters
b) @ParameterizedTest
c) @TestWithParams
d) @DataDrivenTest
Answer 12
b) @ParameterizedTest
13. Which annotation is used to run a piece of code before each test method?
Choose one option
a) @Before
b) @Setup
c) @Init
d) @PreTest
Answer 13
a) @Before
The @Before annotation allows a specific method to run before each test method, often used to set up test preconditions.
14. What is the purpose of the @After annotation in JUnit?
Choose one option
a) To clean up resources after all tests
b) To execute tests in sequence
c) To run code after each test method
d) To initialize test data
Answer 14
c) To run code after each test method
15. In which version of JUnit was the Jupiter API introduced?
Choose one option
a) JUnit 3
b) JUnit 4
c) JUnit 5
d) JUnit 2
Answer 15
c) JUnit 5
The Jupiter API, which provides a new programming and extension model for writing tests, was introduced in JUnit 5.
16. What is the purpose of the @Test(expected = Exception.class) annotation?
Choose one option
a) To prevent exception throwing
b) To specify an exception that should be thrown
c) To log exceptions
d) To convert exceptions
Answer 16
b) To specify an exception that should be thrown
This indicates that a test is expected to throw a specific exception, validating error handling.
17. In JUnit, what does the fail() method do when encountered in a test case?
Choose one option
a) Automatically succeeds the test
b) Disregards the error and continues
c) Causes the test to fail unconditionally
d) Logs a warning message but passes
Answer 17
c) Causes the test to fail unconditionally
The fail() method immediately triggers a test failure when executed.
18. How are test cases grouped in JUnit?
Choose one option
a) Using suites
b) With collections
c) Through category annotations
d) By file naming conventions
Answer 18
a) Using suites
JUnit facilitates grouping test cases via test suites, enabling batch execution of related tests.
19. What JUnit feature allows us to assume certain preconditions for the execution of a test?
a) Conditional test execution
b) Expectations
c) Assumptions
d) PreTestConditions
Answer 19
c) Assumptions
Assumptions in JUnit, via the Assume class, can determine whether a test should proceed based on certain preconditions.
20. How do you specify a timeout for a test method in JUnit?
Choose one option
a) Using the timeout attribute in the @Test annotation.
b) By calling the setTimeout() method on the test instance.
c) Using a separate @Timeout annotation before the test method.
d) By wrapping the test method inside a TimeLimited block.
Answer 20
a) Using the timeout attribute in the @Test annotation.
21. Which of the following assertions checks that a condition is true?
a) assertThat()
b) assertEquals()
c) assertTrue()
d) assertNotNull()
Answer 21
c) assertTrue()
22. What functionality does the assertNotNull() method offer?
a) Asserts an object is not null
b) Checks object equality
c) Verifies object types
d) Disallows object creation
Answer 22
a) Asserts an object is not null
assertNotNull() ensures that a specific object reference is not null.
23. Which keyword is used to denote a test case should not execute in JUnit?
a) @SkipTest
b) @Disabled
c) @Postpone
d) @Cancel
Answer 23
b) @Disabled
@Disabled annotation can be added to avoid running specific test cases without deletion.
24. Which version of JUnit introduced lambda functions for assertions?
a) JUnit 3
b) JUnit 4
c) JUnit 5
d) JUnit 6
Answer 24
c) JUnit 5
25. Which of the following is NOT a type of assertion provided by JUnit?
a) Timeout assertions
b) Conditional assertions
c) Exception assertions
d) Repetition assertions
Answer 25
d) Repetition assertions
We would love to hear from you! Please leave your comments and share your scores in the section below