JUnit MCQ (Multiple Choice Questions)

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

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

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

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

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

Answer: c
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.
advertisement
advertisement

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

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

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

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

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

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

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

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

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

Answer: b
Explanation: JUnit hasItem() is a useful matcher with for use with the assertThat method.
advertisement

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

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

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

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

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

Answer: d
Explanation: JUnitMatchers manipulates the default Java objects and thus, extends java.lang.Object.
advertisement

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Answer: a
Explanation: JUnit provides the TestRule Interface which helps define custom JUnit Rules.


Chapterwise Multiple Choice Questions on JUnit

JUnit MCQ - Multiple Choice Questions and Answers

Our 1000+ MCQs focus on all topics of the JUnit subject, covering 100+ topics. This will help you to prepare for exams, contests, online tests, quizzes, viva-voce, interviews, and certifications. You can practice these MCQs chapter by chapter starting from the 1st chapter or you can jump to any chapter of your choice.
  • Exploring Core JUnit
  • Mastering Junit
  • Software Testing Principles
  • Test Coverage and Development
  • Coarse-grained Testing with Stubs
  • Testing with Mock Objects
  • In-Container Testing
  • Running JUnit Tests from Ant
  • Running Juint Tests from Maven2
  • Continuous Integration Tools
  • 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.

  • Exploring Core JUnit
  • Running Parameterized Test
  • JUnit Test Runners
  • 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.

  • Controller Component
  • Testing the Controller Component
  • Testing Exception Handling
  • Timeout Testing
  • 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.

  • Need for Unit Tests
  • Integration Software Testing
  • Functional Software Testing
  • Stress Testing
  • Acceptance Software Testing
  • Types of Unit Tests
  • Black Box Testing
  • White Box 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.

  • Measuring Test Coverage
  • Cobertura
  • Writing Testable Code
  • Test-driven Development
  • 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.

  • Stubs
  • Stubbing an HTTP Connection
  • Jetty as an Embedded Server
  • Stubbing a Web Server’s Resources
  • Stubbing the Connection
  • 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.

  • Mock Objects
  • Unit Testing with Mock Objects
  • Refactoring with Mock Objects
  • Mocking an HTTP Connection
  • Using Mocks as Trojan Horses
  • EasyMock
  • 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.

  • Limitations of Standard Unit Testing
  • Mock Objects Solution
  • In-container Testing
  • Comparing Stubs, Mock Objects and In-container 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.

  • Ant
  • Ant Targets, Projects, Properties and Tasks
  • Putting Ant to the Task
  • Dependency Management with Ivy
  • Creating HTML Reports
  • 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.

  • Maven Basics
  • Setting Up a Maven Project
  • Maven Plug-ins
  • Maven Surefire
  • Maven Downsides
  • 10. JUnit MCQ on Continuous Integration Tools

    The section contains JUnit multiple choice questions and answers on continuous integration.

  • Continuous Integration
  • If you would like to learn "JUnit" thoroughly, you should attempt to work on the complete set of 1000+ MCQs - multiple choice questions and answers mentioned above. It will immensely help anyone trying to crack an exam or an interview.

    Wish you the best in your endeavor to learn and master JUnit!

    Important Links:

    advertisement
    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.