JUnit Questions and Answers – Exploring Core JUnit

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
View Answer

Answer: a
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
View Answer

Answer: b
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?
a) junit.jar
b) hamcrest-core.jar
c) junit.jar and hamcrest-core.jar
d) java-junit.jar
View Answer

Answer: c
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
View Answer

Answer: b
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?
a) String
b) int
c) Object
d) void
View Answer

Answer: d
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. JUnit test files are written in files with which file extension?
a) .junit
b) .test
c) .java
d) .unit
View Answer

Answer: c
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
View Answer

Answer: d
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?
a) @junit
b) @testcase
c) @org.Test
d) @org.junit.Test
View Answer

Answer: d
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
View Answer

Answer: a
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
View Answer

Answer: b
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.