This set of JUnit Multiple Choice Questions & Answers (MCQs) focuses on “Exploring Core JUnit”.
1. JUnit is used for what type of software testing for the Java language?
a) Unit Testing
b) Integration Testing
c) Functional Testing
d) System Testing
Explanation: Unit testing is the testing of an individual unit or group of related units. That is precisely what JUnit is used for.
2. A JUnit Unit Test Case is compulsorily characterised by a/an known _______ and a/an expected _____
a) output, input
b) input, output
c) variable, literal
d) program, variable
Explanation: A JUnit Unit Test Case is characterized by a known input and an expected output, which is predefined. The known input tests a precondition and the expected output a post-condition.
3. To use JUnit in a project we need to add which JAR files on our test classpath?
c) junit.jar and hamcrest-core.jar
Explanation: hamcrest-core.jar has Matchers that JUnit uses for unit testing.
4. What are fixtures in JUnit?
a) Objects that specify when to run a test
b) Fixed state of a set of objects used as a baseline for running tests
c) Bundle of few test cases run together
d) Date objects
Explanation: Tests need to run against the backdrop of set of predefined or known objects. This set of objects is called a test fixture.
5. JUnit test methods must compulsorily return what value?
Explanation: If a JUnit test method is declared to return anything then file will compile successfully. But the execution will fail because JUnit requires all test methods to be declared to return void.
6. JUnit test files are written in files with which file extension?
Explanation: JUnit test files are regular java files with special methods which are referenced via annotations.
7. What is the purpose of assertArrayEquals(“message”, A, B)?
a) Checks that “message” is in both A and B
b) Checks that “message” is in A but not B
c) Checks that “message” is in B but not A
d) Asserts the equality of the A and B arrays
Explanation: Asserts the equality of the A and B arrays. The “message” is displayed to the user.
8. Which annotation implies that a method is a JUnit test case?
Explanation: Annotate a method with @org.junit.Test to imply that it is a JUnit test case.
9. What is a test runner?
a) Used to execute the test file
b) Defines the test file
c) Used to write test cases
d) Used to define annotations
Explanation: Test runner is used for executing the test cases using the runClasses() method of JUnitCore class of JUnit to run the test case.
10. Which methods cannot be tested by JUnit test class?
a) public methods
b) private methods
c) protected methods
d) methods with void return type
Explanation: When a method is declared as “private”, it can only be accessed within the same class. So there is no way to test a “private” method of a target class from any test class.
Sanfoundry Global Education & Learning Series – JUnit.
To practice all areas of Junit, here is complete set of 1000+ Multiple Choice Questions and Answers.