没有合适的资源?快使用搜索试试~ 我知道了~
Spring Framework Testing 读书笔记
需积分: 0 0 下载量 12 浏览量
2023-11-11
11:53:01
上传
评论
收藏 624KB PDF 举报
温馨提示
试读
93页
Spring Framework Testing 摘要
资源推荐
资源详情
资源评论
Unit Testing
一、Mock Objects
Environment
The org.springframework.mock.env package contains mock implementations of the
Environment and PropertySource abstractions
MockEnvironment and MockPropertySource are useful for developing out-of-container tests for code that
depends on environment-specific properties.
JNDI
The org.springframework.mock.jndi package contains a partial implementation of the JNDI
SPI, which you can use to set up a simple JNDI environment for test suites or stand-alone
applications.
If, for example, JDBC DataSource instances get bound to the same JNDI names in test code
as they do in a Java EE container, you can reuse both application code and configuration in
testing scenarios without modification.
.The mock JNDI support in the org.springframework.mock.jndi package is officially deprecated as of Spring
Framework 5.2 in favor of complete solutions from third parties such as .Simple-JNDI
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 targeted at usage with Spring’s Web MVC framework and are
generally more convenient to use than dynamic mock objects (such as ) or
alternative Servlet API mock objects (such as ).
EasyMock
MockObjects
Since Spring Framework 5.0, the mock objects in org.springframework.mock.web are based on the Servlet
4.0 API.
The Spring MVC Test framework builds on the mock Servlet API objects to provide an integration testing
framework for Spring MVC.
Spring Web Reactive
The org.springframework.mock.http.server.reactive package contains mock implementations
of ServerHttpRequest and ServerHttpResponse for use in WebFlux applications.
The org.springframework.mock.web.server package contains a mock ServerWebExchange
that depends on those mock request and response objects.
1.
a.
b.
2.
a.
b.
c.
3.
a.
b.
c.
d.
4.
a.
b.
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 from
ServerHttpRequest to create a modified instance.
In order for the mock response to properly implement the write contract and return a write completion
handle (that is, Mono<Void>),
it by default uses a Flux with cache().then(), which buffers the data and makes it available for
assertions in tests.
Applications can set a custom write function (for example, to test an infinite stream).
WebTestClient
The WebTestClient builds on the mock request and response to provide support for testing WebFlux
applications without an HTTP server.
The client can also be used for end-to-end tests with a running server.
二、Unit Testing Support Classes
General Testing Utilities
The org.springframework.test.util package contains several general purpose utilities for use in
unit and integration testing.
ReflectionTestUtils
ReflectionTestUtils is a collection of reflection-based utility methods.
You can use these methods in testing scenarios where you need to change the value of a
constant, set a non-public field, invoke a non-public setter method, or invoke a non-public
configuration or lifecycle callback method when testing application code for use cases such as the
following:
ORM frameworks (such as JPA and Hibernate) that condone private or protected field access
asopposed to public setter methods for properties in a domain entity.
Spring’s support for annotations (such as @Autowired, @Inject, and @Resource), that
provide dependency injection for private or protected fields, setter methods, and
configurationmethods.
Use of annotations such as @PostConstruct and @PreDestroy for lifecycle callback methods.
AopTestUtils and AopProxyUtils
AopTestUtils is a collection of AOP-related utility methods.
You can use these methods to obtain a reference to the underlying target object hidden behind
one or more Spring proxies.
c.
i.
d.
i.
ii.
e.
i.
ii.
1.
a.
b.
i.
1.
a.
b.
c.
c.
i.
1.
l
For example, if you have configured a 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 direct
access to the underlying mock to configure expectations on it and perform verifications.
For Spring’s core AOP utilities, see AopUtils and AopProxyUtils.
Spring MVC Testing Utilities
ModelAndViewAssert
The org.springframework.test.web package contains ModelAndViewAssert, which you
can use in combination with JUnit, TestNG, or any other testing framework for unit tests
that deal with Spring MVC ModelAndView objects.
Unit testing Spring MVC Controllers
To unit test your Spring MVC Controller classes as POJOs, use ModelAndViewAssert combined with
MockHttpServletRequest, MockHttpSession, and so on from Spring’s Servlet API mocks.
For thorough integration testing of your Spring MVC and REST Controller classes in conjunction with
your WebApplicationContext configuration for Spring MVC, use the Spring MVC Test Framework
instead.
Integration Testing
一、JDBC Testing Support
JdbcTestUtils
The org.springframework.test.jdbc package contains JdbcTestUtils, which is a collection of JDBC-related
utility functions intended to simplify standard database testing scenarios.
Specifically, JdbcTestUtils provides the following static utility methods.
countRowsInTable(..): Counts the number of rows in the given table.
countRowsInTableWhere(..): Counts the number of rows in the given table by using the provided
WHERE clause.
deleteFromTables(..): Deletes all rows from the specified tables.
deleteFromTableWhere(..): Deletes rows from the given table by using the provided WHERE clause.
dropTables(..): Drops the specified tables.
AbstractTransactionalJUnit4SpringContextTests and AbstractTransactionalTestNGSpringContextTests provide
convenience methods that delegate to the aforementioned methods in JdbcTestUtils.
a.
2.
2.
a.
i.
b.
i.
ii.
1.
a.
b.
i.
ii.
iii.
iv.
v.
2.
The spring-jdbc module provides support for configuring and launching an embedded database, which you can
use in integration tests that interact with a database.
二、Annotations
Spring Testing Annotations
@BootstrapWith
@BootstrapWith is a class-level annotation that you can use to configure how the Spring
TestContext Framework is bootstrapped.
Specifically, you can use @BootstrapWith to specify a custom TestContextBootstrapper.
@ContextConfiguration
@ContextConfiguration defines class-level metadata that is used to determine how to
load and configure an ApplicationContext for integration tests.
Specifically, @ContextConfiguration declares the application context resource locations
or the component classes used to load the context.
Resource locations are typically XML configuration files or Groovy scripts located in the classpath,
Component classes are typically @Configuration classes.
@ContextConfiguration("/test-config.xml") 1
class XmlApplicationContextTests { 2
// class body... 3
}4
5
@ContextConfiguration(classes = TestConfig.class) 6
class ConfigClassApplicationContextTests {7
// class body... 8
}9
10
ApplicationContextInitializer
As an alternative or in addition to declaring resource locations or component classes, you can use
@ContextConfiguration to declare ApplicationContextInitializer classes.
@ContextConfiguration(initializers = CustomContextIntializer.class) 1
3.
1.
a.
i.
ii.
b.
i.
ii.
1.
2.
iii.
1.
class ContextInitializerTests { 2
// class body... 3
}4
ContextLoader strategy
You can optionally use @ContextConfiguration to declare the ContextLoader strategy as well.
Note, however, that you typically do not need to explicitly configure the loader, since the default
loader supports initializers and either resource locations or component classes.
The following example uses both a location and a loader
@ContextConfiguration(locations = "/test-context.xml", loader
= CustomContextLoader.class)
1
class CustomLoaderXmlApplicationContextTests { 2
// class body... 3
}4
Inheritance
@ContextConfiguration provides support for inheriting resource locations or configuration classes
as well as context initializers that are declared by superclasses. 继承super class的
@ContextConfiguration
@WebAppConfiguration
@WebAppConfiguration is a class-level annotation that you can use to declare that the
ApplicationContext loaded for an integration test should be a WebApplicationContext.
resource base path
The mere presence of @WebAppConfiguration on a test class ensures that a
WebApplicationContext is loaded for the test, using the default value of "file:src/main/webapp" for
the path to the root of the web application (that is, the resource base path).
The resource base path is used behind the scenes to create a MockServletContext, which serves
as the ServletContext for the test’s WebApplicationContext.
@ContextConfiguration 1
@WebAppConfiguration 2
class WebAppTests { 3
// class body... 4
iv.
1.
2.
3.
v.
1.
c.
i.
ii.
1.
2.
剩余92页未读,继续阅读
资源评论
zeratul
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- pta题库答案c语言之排序4统计工龄.zip
- pta题库答案c语言之树结构7堆中的路径.zip
- pta题库答案c语言之树结构3TreeTraversalsAgain.zip
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
- 以下是一个简化的示例,它使用pygame库来模拟烟花动画的框架.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功