Search Tutorials


JUnit Multiple-Choice Questions (MCQs) | JavaInUse

JUnit Multiple-Choice Questions (MCQs)

Q. What is JUnit?

A Java-based web framework
A unit testing framework for Java
A Java code compiler
A database management system for Java

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

@TestMethod
@TestCase
@Test
@UnitTest

Q. Which of the following is not an assertion method in JUnit?

assertEquals()
assertTrue()
assertNull()
assertExecute()

Q. What is the purpose of the @BeforeEach annotation in JUnit 5?

To run a method before all tests in the class
To run a method before each test method
To run a method after each test method
To run a method after all tests in the class

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

@Ignore
@Disabled
@Skip
@Inactive

Q. What is the purpose of the assertThrows() method in JUnit?

To verify that a method throws an expected exception
To force a method to throw an exception
To catch and ignore exceptions in a test
To assert that a method does not throw any exceptions

Q. Which of the following is true about JUnit test methods?

They must be private
They must return a value
They must have parameters
They can be public void methods

Q. What is the purpose of the @ParameterizedTest annotation in JUnit 5?

To create tests with random parameters
To run a test multiple times with different parameters
To test methods with optional parameters
To ignore parameters in test methods

Q. Which JUnit 5 annotation is used to specify a custom display name for a test?

@TestName
@DisplayName
@CustomName
@Name

Q. What is the purpose of the @TempDir annotation in JUnit 5?

To create a temporary directory for file operations during tests
To specify a directory for storing test results
To mark a directory for deletion after tests
To indicate a directory containing test resources

Q. Which of the following is NOT a lifecycle method annotation in JUnit 5?

@BeforeAll
@AfterEach
@BeforeTest
@AfterAll





Q. What is the purpose of the assertAll() method in JUnit 5?

To combine multiple assertions into a single statement
To assert that all elements in a collection are true
To ensure all tests in a class pass
To assert that a method completes without exceptions

Q. Which JUnit 5 feature allows you to extend the framework with custom behavior?

Test Runners
Extensions
Plugins
Adapters

Q. What is the purpose of the @RepeatedTest annotation in JUnit 5?

To mark a test method for parallel execution
To run a test method multiple times
To retry a failed test
To create a loop within a test method

Q. Which assertion method in JUnit is used to compare floating-point numbers with a delta for precision?

assertEquals(double expected, double actual)
assertEqualsWithPrecision(double expected, double actual, double delta)
assertEquals(double expected, double actual, double delta)
assertAlmostEquals(double expected, double actual, double delta)

Q. What is the purpose of the @Nested annotation in JUnit 5?

To create subclasses of test classes
To group related test methods
To define inner test classes
To nest assertions within each other

Q. Which of the following is true about test execution order in JUnit 5?

Tests are always executed in the order they are defined in the class
Tests are executed in random order by default
Tests are executed alphabetically by method name
Test execution order is not guaranteed unless explicitly specified

Q. What is the purpose of the @Timeout annotation in JUnit 5?

To pause test execution for a specified time
To set a time limit for test execution
To schedule tests to run at a specific time
To measure the execution time of a test

Q. Which of the following is NOT a valid assumption method in JUnit 5?

assumeTrue()
assumeFalse()
assumeNotNull()
assumeThat()

Q. What is the purpose of the @ExtendWith annotation in JUnit 5?

To extend the timeout period for tests
To add custom extensions to a test class or method
To extend a test class with additional test methods
To extend the assertion capabilities of JUnit

Q. Which JUnit 5 feature allows you to dynamically generate test cases at runtime?

Dynamic Tests
Parameterized Tests
Test Factories
Nested Tests

Q. What is the purpose of the assertIterableEquals() method in JUnit 5?

To assert that two iterables are equal in size
To assert that two iterables contain the same elements in the same order
To assert that two iterables have at least one common element
To assert that an iterable is not empty

Q. Which of the following is true about the @TestInstance annotation in JUnit 5?

It specifies the number of instances to create for a test class
It controls the lifecycle of test instances
It creates a new instance for each test method
It allows sharing of instances between test classes

Q. What is the purpose of the @DisabledOnOs annotation in JUnit 5?

To disable tests on specific operating systems
To run tests only on specific operating systems
To specify the operating system for test execution
To check the operating system version in tests

Q. Which method is used to group assertions in JUnit 5?

assertGroup()
assertMultiple()
assertAll()
assertCombined()

Q. What is the purpose of the @TempDir annotation in JUnit 5?

To create a temporary directory for file operations during tests
To specify a directory for storing test results
To mark a directory for deletion after tests
To indicate a directory containing test resources

Q. Which of the following is NOT a valid order annotation for test method execution in JUnit 5?

@Order
@Priority
@TestMethodOrder
@OrderWith

Q. What is the purpose of the @Disabled annotation in JUnit 5?

To mark a test as completed
To skip a test method or class
To indicate a test is not yet implemented
To disable assertions in a test

Q. Which assertion method in JUnit 5 is used to verify that two objects refer to the same object?

assertSame()
assertEquals()
assertEquivalent()
assertIdentical()

Q. What is the purpose of the @ParameterizedTest annotation in JUnit 5?

To create tests with random parameters
To run a test multiple times with different parameters
To test methods with optional parameters
To ignore parameters in test methods

Q. Which of the following is NOT a valid source for parameterized tests in JUnit 5?

@ValueSource
@EnumSource
@ArraySource
@CsvSource

Q. What is the purpose of the @DisplayName annotation in JUnit 5?

To set the name of the test class
To provide a custom name for a test method or class
To display the test result in the console
To show the execution time of a test