Here are 1000 MCQs on JUnit (Chapterwise).
1. Which of the following is not correct about JUnit?
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
View Answer
Explanation: JUnit test can be run automatically and they check their own results and provide immediate feedback.
2. JUnit is used for what type of software testing for the Java language?
a) Functional Testing
b) System Testing
c) Unit Testing
d) Integration Testing
View Answer
Explanation: Unit testing is the testing of an individual unit or group of related units. That is precisely what JUnit is used for.
3. Which annotation implies that a method is a JUnit test case?
a) @org.junit.Test
b) @org.Test
c) @testcase
d) @junit
View Answer
Explanation: Annotate a method with @org.junit.Test to imply that it is a JUnit test case.
4. JUnit test files are written in files with which file extension?
a) .unit
b) .java
c) .test
d) .junit
View Answer
Explanation: JUnit test files are regular java files with special methods which are referenced via annotations.
5. Which methods cannot be tested by the JUnit test class?
a) protected methods
b) methods with void return type
c) private methods
d) public methods
View Answer
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 JUnit test class.
6. JUnit runners are available in which package?
a) org.junit.files.runners
b) org.runners
c) junitfiles.runners
d) org.junit.runners
View Answer
Explanation: The default JUnit package is “org,junit”. Runners are found in “org,junit.runner”.
7. What is a Façade in JUnit?
a) Façade is a design pattern that provides a unified interface to a set of interfaces in a subsystem
b) Façade is a group of test cases
c) Façade is a group of mock objects
d) Façade is a collection of supercases
View Answer
Explanation: In Junit, Façade defines a higher-level interface that makes the subsystem easier to use.
8. What does the fail() method do in JUnit?
a) Outputs the message “Fail” to the console
b) Pauses the test for 1 second
c) Throws an assertion error unconditionally
d) Calls the default constructor
View Answer
Explanation: The method throws an assertion error unconditionally. This might be helpful to show an incomplete test (maybe still being worked upon) or to ensure that an expected exception is thrown.
9. To run the file TestClass.class from the command line, we have to type what?
a) org.junit.runner.JUnitCore TestObject
b) java org.junit.runner.JUnitCore TestClass
c) javac TestCase
d) org.junit.java TestCase
View Answer
Explanation: The test cases are executed using JUnitCore class which is referenced by “org.junit.runner.JUnitCore”.
10. JUnit Suites are independent of the capability of the ______ system.
a) Build
b) Test
c) Method
d) Run
View Answer
Explanation: JUnit Suites are useful to organize tests in Java, independent of the capability of the build system, because it’s common for someone or a group other than the developers to maintain builds.
11. Which of the following is an example of a web controller?
a) Php
b) Java
c) JUnit
d) Apache Struts Framework
View Answer
Explanation: Apache Struts Framework is an open-source framework for building Servlet/JSP based web applications which is an example of a web controller.
12. Which of the following is not a mocking framework?
a) Mockito
b) EasyMock
c) MockJava
d) PowerMock
View Answer
Explanation: EasyMock, jMock, Mockito, Unitils Mock, PowerMock and JMockit are various mocking framework.
13. Which of the following is correct about Test Suite in JUnit?
a) @RunWith annotation is used to run the suite test
b) Test suite means bundle a few unit test cases and run them together
c) @Suite annotation is used to run the suite test
d) All of the Mentioned
View Answer
Explanation: Test suite allows us to aggregate a few unit test cases from multiple classes in one place and run them together. @RunWith and @Suite annotation are used to run the suite test.
14. The JUnit hasItem() function is a _____________
a) Matcher
b) JUnitMatcher
c) Instance Function
d) Hamcrest Matcher
View Answer
Explanation: JUnit hasItem() is a useful matcher with for use with the assertThat method.
15. Which of the following is also known as Beta Testing?
a) Unit testing
b) Functional testing
c) Box testing
d) Field testing
View Answer
Explanation: Beta testing is done at the customer’s location leading to it being also called field testing.
16. JUnit test methods must compulsorily return what value?
a) void
b) Object
c) String
d) int
View Answer
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.
17. Which of the following is a free Java tool that calculates the percentage of code accessed by tests?
a) Maven
b) XUnit
c) JUnit
d) Cobertura
View Answer
Explanation: Cobertura is used to identify which parts of the Java program are lacking test coverage.
18. Which package contains interfaces that define jMock’s Domain Specific Embedded Language?
a) org.jmock.integration.junit3
b) org.jmock.lib.junit3
c) org.jmock
d) org.jmock.syntax
View Answer
Explanation: org.jmock.syntax defines interfaces that define jMock’s Domain Specific Embedded Language.
19. Class JUnitMatchers extends _______________
a) java.lang.String
b) java.JUnit.util.*
c) java.util.Arrays
d) java.lang.Object
View Answer
Explanation: JUnitMatchers manipulates the default Java objects and thus, extends java.lang.Object.
20. Which of the following test has to be completed before integration testing?
a) Unit testing
b) Load testing
c) Stress testing
d) Functional testing
View Answer
Explanation: Once the test for a class are up and running, the next step is to hook up the class with other methods and services.
21. “cobertura-check.bat” is run ________ the JUnit tests to show which classes do not have adequate test coverage.
a) Before and after
b) Along with
c) After
d) Before
View Answer
Explanation: This is used to check test coverage and hence, can only be run after the JUnit tests.
22. Which of the following is the basic unit of development in Scrum?
a) Slice
b) Sprint
c) Cobertura
d) Manifesto
View Answer
Explanation: In Scrum, each iteration is also known as a Sprint, which is the basic unit of development.
23. Which of the following translates ExpectationErrors into JUnit AssertionFailedErrors?
a) VerifyingTestCase
b) MockObjectTestCase
c) JUnit3Mockery
d) JUnit3ErrorTranslator
View Answer
Explanation:JUnit3ErrorTranslator translate Expectation Errors into JUnit AssertionFailedErrors.
24. Which of the following is a JUnit Rule that manages JMock expectations and allowances?
a) JunitRule
b) JunitRuleMockery
c) JunitMockery
d) JmockRuleMockery
View Answer
Explanation: JunitRuleMockery asserts that expectations have been met after each test has finished.
25. What are fixtures in JUnit?
a) Fixed state of a set of objects used as a baseline for running tests
b) Date objects
c) Objects that specify when to run a test
d) Bundle of few test cases run together
View Answer
Explanation: Tests need to run against the backdrop of a set of predefined or known objects. This set of objects is called a test fixture.
26. What is the full form of JJ in “JJ-path”?
a) Jump-to-Jump
b) Java-JUnit
c) Jump-in-JUnit
d) Just Jump
View Answer
Explanation: JJ-path or Jump-to-Jump-path is a well-defined linear region of a program’s code.
27. Which of the following is a Java HTTP (Web) server and Java Servlet container?
a) Ant
b) Maven
c) Zimbra
d) Jetty
View Answer
Explanation: Jetty is often used for machine to machine communications, usually within larger software frameworks.
28. To use JUnit in a project we need to add which JAR files on our test classpath?
a) java-junit.jar
b) junit.jar and hamcrest-core.jar
c) hamcrest-core.jar
d) junit.jar
View Answer
Explanation: hamcrest-core.jar has Matchers that JUnit uses for unit testing.
29. What type of object is returned on completion of a test?
a) org.junit.runner.Object
b) org.junit.runner.Outcome
c) org.junit.runner.Complete
d) org.junit.runner.Result
View Answer
Explanation: An org.junit.runner.Result object collects and summarizes information from running multiple tests.
30. Mocks are Trojan horses because they replace real ___________ from the inside, without the calling classes being aware of it.
a) Objects
b) Variables
c) Classes
d) Methods
View Answer
Explanation: Mocks have access to internal information about the class, making them quite powerful.
31. Stubs usually provide high _____________ in the tested system.
a) Mocking
b) Security
c) Confidence
d) Modularity
View Answer
Explanation: With stubs, the objects under test aren’t getting modified.
32. In which of the following test, Monkey Testing is also included in Android Studio as part of the standard testing tools.
a) Integration Testing
b) Functional Testing
c) Stress Testing
d) Unit Testing
View Answer
Explanation: Monkey testing is used to implement stress testing on the Android Studio.
33. For a Suite class, the @RunWith annotation has the value of which class?
a) org.runners.Suite.class
b) org.junit.runners.Suite.class
c) org.junit.runners.result
d) org.junit.Suite.cases
View Answer
Explanation: It contains the definition for the Suite class and a the declaration is @RunWith(value=org.junit.runners.Suite.class).
34. Which testing is used for testing systems for which the specification takes in the form of rules or cause-effect combinations?
a) Load Testing
b) Decision Table Testing
c) Unit Testing
d) Functional Testing
View Answer
Explanation: In a decision table the inputs are listed down in the form of a column, while the outputs are in the same column but below the inputs.
35. Which of the following rules helps to set up custom JUnit rules?
a) TestRule Interface
b) CreateRule Interface
c) RuleCreate Interface
d) RuleInitialize Interface
View Answer
Explanation: JUnit provides the TestRule Interface which helps define custom JUnit Rules.
Chapterwise Multiple Choice Questions on JUnit
1. MCQ on Exploring Core JUnit
The section contains JUnit multiple choice questions and answers on exploring core JUnit, running parameterized test, JUnit Test Runners and composing test with a Suite.
|
|
2. Multiple Choice Questions on Mastering JUnit
The section contains JUnit questions and answers on the controller component and testing the component, testing exception handling, timeout testing and hamcrest matchers.
|
|
3. JUnit MCQ on Software Testing Principles
The section contains JUnit MCQs on types of testing like unit tests, black box, white box, stress, integration, functional and acceptance software testing.
|
|
4. JUnit MCQ on Test Coverage and Development
The section contains JUnit multiple choice questions and answers on measuring test coverage, cobertura, writing testable code, test-driven development and testing in the development cycle.
|
|
5. JUnit MCQ on Coarse-grained Testing with Stubs
The section contains JUnit questions and answers on stubs, stubbing the connection, http connection as well as web server resources and jetty as an embedded server.
|
|
6. JUnit Multiple Choice Questions on Testing with Mock Objects
The section contains JUnit MCQs on mock objects, testing and refactoring with mock objects, mocking an http connection, using mock as trojan horses, easymock and jmock.
|
|
7. JUnit MCQ on In-container Testing
The section contains JUnit multiple choice questions and answers on mock objects and solutions, in-container testing and limitations of unit testing.
|
|
8. MCQ on Running JUnit Tests from Ant
The section contains JUnit questions and answers on ant-targets, projects, tasks, creating html reports and batching tests.
|
|
9. Multiple Choice Questions on Running JUnit Tests from Maven2
The section contains JUnit MCQs on maven introduction, plugins, surefire, downsides and also setting up maven project.
|
|
10. JUnit MCQ on Continuous Integration Tools
The section contains JUnit multiple choice questions and answers on continuous integration.
|
Wish you the best in your endeavor to learn and master JUnit!
Important Links:
- JUnit Best Books
- Java Multiple Choice Questions
- Spring Multiple Choice Questions
- Programming Multiple Choice Questions
- Computer Science Multiple Choice Questions