Spring Questions and Answers – Unit Testing Spring

This set of Java Spring Multiple Choice Questions & Answers (MCQs) focuses on “Unit Testing Spring”.

1. This encapsulates the context of a test’s execution:-
a) Test context
b) Test context manager
c) Test context listener
d) Test execution listener
View Answer

Answer: a
Explanation: This encapsulates the context of a test’s execution, including the application context, test class, current test instance, current test method, and current test exception.

2. This manages a test context for a test and triggers test execution listeners:-
a) Test context
b) Test context manager
c) Test context listener
d) Test execution listener
View Answer

Answer: b
Explanation: This manages a test context for a test and triggers test execution listeners at predefined test execution points, including when preparing a test instance, before executing a test method (before any framework-specific initialization methods), and after executing a test method (after any framework-specific cleanup methods).

3. This defines a listener interface; by implementing this, you can listen to test execution events.
a) Test context
b) Test context manager
c) Test context listener
d) Test execution listener
View Answer

Answer: d
Explanation: The TestContext framework provides several test execution listeners for common testing features, but you are free to create your own.
advertisement
advertisement

4. Spring provides convenient TestContext support classes for:-
a) JUnit3
b) JUnit4
c) TestNG5
d) All of the mentioned
View Answer

Answer: d
Explanation: Spring provides convenient TestContext support classes for JUnit 3, JUnit 4, and TestNG 5, with particular test execution listeners pre registered.

5. JUnit 4 allows you to annotate your test methods with:-
a) @Test
b) @Autowire
c) @JTest
d) None of the mentioned
View Answer

Answer: a
Explanation: JUnit 4 allows you to annotate your test methods with JUnit @Test annotation, so an arbitrary public method can be run as a test case.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Usually, a test and its target class are located in the same package, but the source files of tests are stored in a separate directory.
a) True
b) False
View Answer

Answer: a
Explanation: (e.g., test) from the source files of other classes (e.g., src).

7. Method to perform cleanup tasks:-
a) finalize
b) tearDown
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: c
Explanation: You can override the tearDown() method to perform cleanup tasks, such as releasing permanent resources.
advertisement

8. The static assert methods is declared in the:-
a) org.junit.Assert
b) org.junit.*
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: c
Explanation: You can override the tearDown() method to perform cleanup tasks, such as releasing permanent resources.

9. Attribute which specifies the exception type:-
a) after
b) expected
c) before
d) test
View Answer

Answer: b
Explanation: JUnit 4 offers a powerful feature that allows you to expect an exception to be thrown in a test case. You can simply specify the exception type in the expected attribute of the @Test annotation.
advertisement

10. Test data sets are provided by data providers, which are methods with the:-
a) @DataProvider
b) @Autowire
c) @JTest
d) None of the mentioned
View Answer

Answer: a
Explanation: In TestNG, test data sets are provided by data providers, which are methods with the @DataProvider annotation.

11. An object that simulates a dependent object:-
a) stub
b) mock
c) test
d) none of the mentioned
View Answer

Answer: a
Explanation: A stub is an object that simulates a dependent object with the minimum number of methods required for a test.

12. An Object which usually knows how its methods are expected to be called:-
a) stub
b) mock
c) test
d) none of the mentioned
View Answer

Answer: b
Explanation: In contrast to a stub, a mock object usually knows how its methods are expected to be called in a test.

13. Libraries that can help create mock objects:-
a) EasyMock
b) jMock
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: In Java, there are several libraries that can help create mock objects, including EasyMock and jMock.

14. Test used to test several units in combination as a whole.
a) Integration tests
b) JUnit4
c) TestNG5
d) All of the mentioned
View Answer

Answer: a
Explanation: Integration tests, in contrast, are used to test several units in combination as a whole.

15. Spring supports web controller testing by providing Servlet API:-
a) MockHttpServletRequest
b) MockHttpServletResponse
c) MockHttpSession
d) All of the mentioned
View Answer

Answer: d
Explanation: Spring supports web controller testing by providing a set of mock objects for the Servlet API (including MockHttpServletRequest, MockHttpServletResponse, and MockHttpSession).

Sanfoundry Global Education & Learning Series – Java Spring.
To practice all areas of Java Spring, 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]

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