发布日期:2019-05-30 11:01:17

本文是学习Spring Testing官方文档的知识点摘要。官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testing

关键词:Mock Objects, TestContext Framework, Spring MVC Test, WebTestClient

 

1. Introduction to Spring Testing

2. Unit Testing

DI(Dependency Injection) make code less dependent on the container. POJO should be testable in JUnit or TestNG tests. True unit tests run extremely quickly.

  • Mock Objects
    • Spring includes a number of packages dedicated to mocking:
      • Environment
        • org.springframework.mock.env
        • Environment
        • PropertySource
        • MockEnvironment
        • MockPropertySource
      • JNDI
        • org.springframework.mock.jndi
      • Servlet API
      • Spring Web Reactive
        • org.springframework.mock.http.server.reactive
        • ServerHttpRequest
        • ServerHttpResponse
        • org.springframework.mock.web.server
        • ServerWebExchange
        • MockServerHttpRequest
        • MockServerHttpRespose
        • WebTestClient
  • Unit Testing Support Classes
    • General Testing Utilities
      • org.springframework.test.util
      • ReflectionTestUtils
      • AopTestUtils
      • AopUtils
      • AopProxyUtils
    • Spring MVC Testing Utilities
      • org.springframework.test.web
      • ModelAndViewAssert
      • Junit & TestNG
      • Servlet API Mock
      • Spring MVC Test Framework

3. Integration Testing

  • Overview
    • Test correct wiring of your Spring IoC container contexts
    • Data access using JDBC or an ORM tool. This can include such things as the correctness of SQL statements, Hibernate queries, JPA entity mappings, and so forth.
    • org.springframework.test
  • Goals of Integration Testing
    • Primary Goals
      • To manage Spring IoC container caching between tests
      • To provide Dependency Injection of test fixture instances.
      • To provide transaction management appropriate to integration testing
      • To support Spring-specific base classes that assist developers in writing integration tests.
    • Context Management and Caching
    • Dependency Injection of Test fixtures
    • Transaction management
      • PlatformTransactionManager
    • Support classes for Integration Testing
  • JDBC Testing Support
    • org.springframework.test.jdbc.JdbcTestUtils
      • countRowsIntable()
      • countRowsInTableWhere()
      • deleteFromTables()
      • deleteFromTableWhere()
      • dropTables()
  • Annotations
    • Spring Testing Annotations
      • @BootstrapWith, @ContextConfiguration, @WebAppConfiguration, @ContextHierarchy, @ActiveProfiles, @TestPropertySource,  @DirtiesContext, @TestExecutionListeners, @Commit, @Rollback, @BeforeTransaction, @AfterTransaction, @Sql, @SqlConfig, @SqlGroup
    • Standard Annotation Support
      • @Autowired, @Qualifier, @Resource, @ManagedBean, @Inject, @Named, @PersistenceContext, @PersistenceUnit, @Required, @Transactional
    • Spring JUnit 4 Testing Annotations
      • (Supported only when used in conjunction with SpringRunner, Spring's JUnit 4 rules)
      • @IfProfileValue, @ProfileValueSourceConfiguration, @Timed, @Repeat
    • Spring JUnit Jupiter Testing Annotations
      • Supported only when used in conjunction with SpringExtension and JUnit Jupiter.
      • @SpringJUnitConfig, @SpringJUnitWebConfig, @EnabledIf, @DisabledIf
    • Meta-Annotations Support for Testing
  • Spring TestContext Framework
    • Key Abstractions
    • Bootstrapping the TestContect Framework
    • TestExecutionListener Configuration
    • Context Management
    • Dependency Injection of Test Fixtures
    • Testing Request- and Session- scoped Beans
    • Transaction Management
    • Executing SQL Scripts
    • Parallel Test Execution
    • TestContext Framework Support Classes
  • Spring MVC Test Framework
    • Server-Side Tests
    • HtmlUnit Integration
    • Client-Side REST Tests
  • WebTestClient
    • Setup
    • Writing Tests

4. Further Resources

  • JUnit: “A programmer-oriented testing framework for Java”. Used by the Spring Framework in its test suite.
  • TestNG: A testing framework inspired by JUnit with added support for annotations, test groups, data-driven testing, distributed testing, and other features.
  • AsssertJ: “Fluent assertions for Java”, including support for Java 8 lambdas, streams, and other features.
  • Mock Objects
  • MockObjects.com: Web site dedicated to mock objects, a technique for improving the design of code within test-driven development.
  • Mockito: Java mock library based on the Test Spy pattern.
  • EasyMock:Java library “that provides Mock Objects for interfaces (and objects through the class extension) by generating them on the fly using Java’s proxy mechanism.” Used by the Spring Framework in its test suite.
  • JMock: Library that supports test-driven development of Java code with mock objects.
  • DbUnit: JUnit extension (also usable with Ant and Maven) that is targeted at database-driven projects and, among other things, puts your database into a known state between test runs.
  • The Grinder: Java load testing framework.

发表评论