Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the contents of the Spring test

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the content of the Spring test". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the content of the Spring test"?

1 introduction to Spring testing

Testing is an integral part of enterprise software development. This chapter focuses on the added value that IoC principles bring to unit testing, as well as the benefits of Spring framework support for integration testing. (thorough handling of testing in the enterprise is beyond the scope of this reference manual. )

2 unit test

Dependency injection should make your code less dependent on containers than traditional Java EE development. The POJO that makes up the application should be able to be tested in JUnit or TestNG tests, and objects can be instantiated using the new operator without using Spring or any other container. You can use the mock object (in combination with other valuable testing techniques) to test the code individually. If you follow Spring's architectural recommendations, the ultimate 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 having to access persistent data when running unit tests.

Real unit tests usually run very fast because there is no runtime infrastructure that can be set up. Using real unit testing as part of a development approach can improve productivity. You may not need this part of the testing chapter 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:

Environment

JNDI

Servlet API

Spring Web Reactive

2.1.1 Environment

The org.springframework.mock.env package contains Environment and PropertySource abstract mock implementations. MockEnvironment and MockPropertySource are useful for developing out-of-container tests for code that depends on specific environment attributes.

2.1.2 JNDI

The org.springframework.mock.jndi package contains a partial implementation of JNDI SPI that can be used to set up a simple JNDI environment for test suites or stand-alone applications. For example, if the JDBC DataSource instance is bound to the same JNDI name in the test code as it is in the Java EE container, you can reuse the application code and configuration in the test scenario without modification.

Starting with the Spring Framework 5.2, mock JNDI support in the org.springframework.mock.jndi package has been officially deprecated and a complete solution from a third party, such as Simple-JNDI, is desired.

2.1.3 Servlet API

The org.springframework.mock.web 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 generally more convenient to use than dynamic mock objects (such as EasyMock) or other Servlet API mock objects (such as MockObjects).

Starting with Spring Framework 5.0, the simulation objects in org.springframework.mock.web are based on Servlet 4.0 API.

The Spring MVC testing framework is based on simulating Servlet API objects and provides an integrated testing framework for Spring MVC.

2.1.4 Spring Web Reactive

The org.springframework.mock.http.server.reactive package contains mock implementations of ServerHttpRequest and ServerHttpResponse for use by WebFlux applications. The org.springframework.mock.web.server package contains a mock ServerWebExchange that relies on those mock request and response objects.

Both MockServerHttpRequest and MockServerHttpResponse extend from the same abstract base classes as server-specific implementations and share behavior with them. For example, a mock request is immutable once created, but you can use the mutate () method in ServerHttpRequest to create a modified instance.

In order for the mock response to correctly implement the write contract and return the write completion handle (that is, Mono), by default it uses cache (). Then () with Flux to buffer the data and make it available for assertions in the test. The application can set up custom authoring functions (for example, testing an infinite stream).

WebTestClient is based on simulating requests and responses and provides support for testing WebFlux applications that do not use HTTP servers. The client can also be used for end-to-end testing of the running server.

2.2 Unit Test support classes

Spring contains a number of classes that can help with unit testing. They fall into two categories:

General testing tool

Spring MVC testing tool

2.2.1 General testing tools

The org.springframework.test.util package contains several general utilities for unit testing and integration testing.

ReflectionTestUtils is 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 field access for private or protected, rather than public setter methods for attributes in domain entities.

Spring supports annotations (such as @ Autowired, @ Inject, and @ Resource) that provide dependency injection for private or protected fields, setter methods, and configuration methods.

Use annotations such as @ PostConstruct and @ PreDestroy for lifecycle callback methods.

AopTestUtils is a collection of utility methods related to AOP. You can use these methods to obtain references to underlying target objects hidden behind one or more Spring proxies. For example, if you have configured bean as a dynamic mock by using a library such as EasyMock or Mockito, and the mock is wrapped in a Spring proxy, you may need to access the underlying mock directly to configure expectations and perform validation on it. For the core AOP utility for Spring, see AopUtils and AopProxyUtils.

2.2.2 Spring MVC testing tool

The org.springframework.test.web package contains ModelAndViewAssert, which you can use in conjunction with JUnit, TestNG, or any other testing framework for unit testing that deals with Spring MVC ModelAndView objects.

Unit test Spring MVC controller

To unit test the Spring MVC Controller class as POJO, use ModelAndViewAssert with MockHttpServletRequest, MockHttpSession, and so on in Spring's Servlet API simulation. For comprehensive integration testing of Spring MVC and REST Controller classes with Spring MVC's WebApplicationContext configuration, use the Spring MVC test framework instead.

Thank you for your reading, the above is the content of "what is the content of the Spring test?" after the study of this article, I believe you have a deeper understanding of what the Spring test content has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report