- Spring Essentials
- Shameer Kunjumohamed Hamidreza Sattari
- 389字
- 2021-07-16 13:05:49
Testing with Spring
The degree of testability shows the elegance and maturity of any framework. A more testable system is more maintainable. Spring Framework provides comprehensive support for end-to-end testing of applications for both unit testing as well as integration testing. Spring promotes test-driven development (TDD), facilitates integration testing, and advocates a set of best practices for the unit testing of beans. This is another compelling reason for using Spring to build serious applications.
The POJO-based programming model and loosely coupled nature of Spring beans make it easier to participate in JUnit and TestNG tests even without Spring in the middle. On top of this, Spring provides many testing support components, utilities, and mock objects to make the testing easier.
Mock objects
Spring provides mock implementations of many container-specific components so that the beans can be tested outside a server or container environment. MockEnvironment
and MockPropertySource
are useful for testing environment-dependent beans. To test beans that depend on HTTP communications, Spring provides mock classes for both client and server sides inside the org.springframework.mock.http
and org.springframework.mock.http.client
packages.
Another set of useful classes can be found under org.springframework.mock.jndi
to run test suites that depend on JNDI resources. The org.springframework.mock.web
package contains mock objects for web components based on Servlet 3.0, such as web contexts, filters, controllers, and asynchronous request processing.
Unit and integration testing utilities
Spring ships certain general-purpose and context-specific utilities for unit and integration testing. The org.springframework.test.util
package contains a set of utility classes for various testing purposes, including reflection, AOP, JSON, and XML manipulations. Classes under org.springframework.test.web
and its nested subdirectories contain a comprehensive set of utility classes to test beans dependent on the web environment. Another set of useful classes for usages specific to ApplicationContext
can be found under org.springframework.test.context
and its child packages. Their support includes the loading and caching of web, portlet, or application contexts in the test environment; resolving profiles; loading property sources and SQL scripts; managing transactions for test environments; and so on.
The support classes and annotations under the packages listed earlier facilitate the easy and natural testing of Spring applications. A comprehensive discussion over Spring test support is beyond the scope of this book. However, gaining a good understanding of Spring's comprehensive support for unit and integration tests is vital in order to develop elegant code and maintainable applications using Spring.
- Clojure Programming Cookbook
- Flutter開發(fā)實戰(zhàn)詳解
- 教孩子學(xué)編程:C++入門圖解
- Unity 5 for Android Essentials
- 數(shù)據(jù)結(jié)構(gòu)與算法分析(C++語言版)
- 愛上micro:bit
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- JavaScript動態(tài)網(wǎng)頁編程
- Python機器學(xué)習(xí)算法與應(yīng)用
- HTML5+CSS3+JavaScript 從入門到項目實踐(超值版)
- iOS Development with Xamarin Cookbook
- C語言編程魔法書:基于C11標(biāo)準(zhǔn)
- 絕密原型檔案:看看專業(yè)產(chǎn)品經(jīng)理的原型是什么樣
- Learning Perforce SCM
- 歐姆龍PLC編程指令與梯形圖快速入門