Spring core chapter:
Spring 5 Chinese parsing core – IOC container dependency
Spring 5 Chinese parsing core – bean scope of IOC container
Spring 5 Chinese parsing core part – Custom bean properties of IOC container
Spring 5 Chinese parsing core part – beandefinition inheritance and container expansion point of IOC container
Annotation based container configuration of IOC container
Spring 5 Chinese parsing core – path scanning and component management of IOC container
Spring 5 Chinese parsing core part – jsr330 standard annotation of IOC container
Spring 5 Chinese parsing core – IOC container based on Java container configuration
Spring 5 Chinese parsing core – environment abstraction of IOC container
Application context and beanfactory of IOC container
Spring 5 Chinese parsing core – resources of IOC container
Spring 5 Chinese parsing core – data validation, data binding and type conversion of IOC container
Spring 5 Chinese parsing core chapter – the spice expression of IOC container
AOP programming of IOC container (Part 1)
AOP programming of IOC container (2)
Spring 5 Chinese parsing core – spring AOP API of IOC container
The previous spring core has been completed. Please check the previous article. Let’s move on to the testing section in spring.
This chapter introduces spring’s support for integration testing and best practices for unit testing. The spring team advocates Test Driven Development (TDD). Spring’s team found that the proper use of inversion of control (IOC) is indeed a simplification of unit and integration testing (the presence of setter methods and appropriate constructor classes makes it easier to wire them together for testing without having to establish a service locator registry and similar structures).
1 Introduction to spring testing
Testing is an integral part of enterprise software development. This chapter focuses on the added value of IOC principles for unit testing and the benefits of spring framework’s support for integration testing. (thorough treatment of testing in the enterprise is not within the scope of this reference manual.)
2 unit test
Compared with traditional Java EE development, dependency injection should make your code less dependent on containers. The POJOs that make up your application should be testable in JUnit or TestNG tests, and you can instantiate objects using the new operator without using spring or any other container. You can use mock objects (combined with other valuable testing techniques) to test code individually. If you follow spring’s architecture recommendations, the final layering and Componentization of the code base will make unit testing easier. For example, you can test service layer objects through stubs or mock Dao or repository interfaces without accessing persistent data while running unit tests.
Real unit tests usually run very fast because there is no runtime infrastructure to set up. Using real unit testing as part of the development approach can improve productivity. You may not need this part of the testing section to help you write effective unit tests for IOC based applications. However, for some unit test scenarios, the spring framework provides mock objects and test support classes, which are described in this chapter.
2.1 mock object
Spring contains many packages dedicated to mock:
- Servlet API
- Spring Web Reactive
org.springframework.mock.envThe package contains
PropertySourceAbstract mock implementation (see bean definition profile and propertysource abstraction).
MockPropertySourceIt is useful for developing < U > out of container tests < / u > for code that depends on specific environment properties.
org.springframework.mock.jndiThe package contains a partial implementation of JNDI SPI and can be used to set up a simple JNDI environment for a test suite or stand-alone application. For example, if JDBC
DataSourceIf the instance is bound to the same JNDI name in the test code as in the Java EE container, the application code and configuration can be reused in the test scenario without modification.
Starting with spring framework 5.2, it was officially abandoned
org.springframework.mock.jndiMock JNDI support in the package, but want to use a complete third-party solution, such as
2.1.3 Servlet API
org.springframework.mock.webThe package contains a comprehensive set of servlet API mock objects that are useful for testing web contexts, controllers, and filters. These mock objects are used for spring’s Web MVC framework, and are usually more than dynamic mock objects (for example
EasyMock）Or other servlet API mock objects (for example
MockObjects）More convenient to use.
Starting with spring framework 5.0,
org.springframework.mock.webThe mock object in is based on servlet 4.0 API.
Spring MVC testing framework is based on the simulation of servlet API objects, which provides an integration testing framework for spring MVC. See the spring MVC test framework.
2.1.4 Spring Web Reactive
org.springframework.mock.http.server.reactiveThe package contains
ServerHttpResponseFor the implementation of mock
org.springframework.mock.web.serverThe package contains a simulation
ServerWebExchangeIt depends on those mock request and response objects.
MockServerHttpResponseBoth extend from the same abstract base classes as server specific implementations and share behavior with them. For example, mock requests are immutable once created, but you can use the
mutate()Method to create a modified instance.
In order for the simulated response to implement the write contract correctly and return the write completion handle (that is, mono < void >), by default, it uses the
cache().then()To buffer the data and make it available for assertion in the test. Applications can set up custom writing functions (for example, to test an infinite stream).
WebTestClientBased on the simulation of requests and responses, the
WebFluxApplication testing provides support. The client can also be used for end-to-end testing of a running server.
2.2 unit test support class
Spring contains many classes that can help with unit testing. They are divided into two categories:
- General testing tools
- Spring MVC testing tool
2.2.1 general test tools
org.springframework.test.utilThe package contains several common utilities for unit testing and integration testing.
ReflectionTestUtilsIs a collection of reflection based utility methods. You can use these methods in test scenarios where you need to change constant values, set non-public fields, call non-public setter methods, or call non-public configuration or lifecycle callback methods
- ORM frameworks such as JPA and Hibernate allow
protectedInstead of the properties in the domain entity
- Spring supports annotations (for example
@Resource）, these notes are
protectedField of the,
setterMethods and configuration methods provide dependency injection.
- Use for example
@PreDestroyAnnotations like this are used for lifecycle callback methods.
Aoptestutils is a collection of utility methods related to AOP. You can use these methods to get references to underlying target objects hidden behind one or more spring proxies. For example, if you have already used the library (e.g
Mockito）Configure beans to be dynamic
mock, and the
mockWrapped in a spring proxy, you may need to access the underlying
mockTo configure expectations and perform validation on them. For spring’s core AOP utilities, see aoputils and aopproxyutils.
2.2.2 spring MVC testing tool
org.springframework.test.webThe package contains
ModelAndViewAssertYou can use it with JUnit, TestNG, or any other testing framework to handle spring MVC
Unit test spring MVC controller
For spring MVC as a POJO
ControllerClass for unit testing, please set the
ModelAndViewAssertAnd spring’s servlet API simulation
MockHttpSessionAnd so on. In order to work with spring MVC
WebApplicationContextConfigure spring MVC and rest together
ControllerClass for comprehensive integration testing, use the spring MVC test framework instead.
Personally engaged in the financial industry, I have worked in Chongqing’s first-class technical team, such as yijifu, Sijian technology, a car Hailing platform, etc. at present, I am in charge of the construction of unified payment system in a bank. I have a strong interest in the financial industry. At the same time, it also practices big data, data storage, automatic integration and deployment, distributed microservices, responsive programming, artificial intelligence and other fields. At the same time, it is also keen to share technology and create official account and blog site. Pay attention to the official account:Young it manGet the latest technology article push!
Blog address: http://youngitman.tech
The official account of WeChat:
Technical exchange group: