ćććÆććŖć«ććććć¦ęøćććć®ļ¼
Springć«é¢ćććć¹ćć®č©±ć«ć¤ćć¦ććć¾ćē„ććŖćć®ć§ć”ćć£ćØč¦ć¦ćććććŖććØęćć¾ćć¦ć
ććć£ćØćSpringć®ćć¹ćć«é¢ććććć„ć”ć³ććēŗćć¦ćæć¾ćć
Springć®ćć¹ćć«é¢ćć話
ä»åę±ćć®ćÆćSpring Bootć®ćć¹ćć«é¢ćć話ćØSpring Frameworkć®ćć¹ćć«é¢ćć話ļ¼Spring Testļ¼ć«ć¤ćć¦č¦ć¦ćććććŖćØęćć¾ćć
åÆ¾č±”ć®ćć¼ćøć§ć³ćÆć仄äøćØćć¾ćć
- Spring Boot 2.6.6
- Spring Framework 5.3.18
Spring Bootć®ćć¹ćć«é¢ććććć„ć”ć³ććÆćć”ćć
Spring Frameworkć®ćć¹ćļ¼Spring Testļ¼ć«é¢ććććć„ć”ć³ććÆććć”ćć
ććććč¦ć¦ććć¾ćććä»åćÆåä½ćć¹ćć«ć¤ćć¦
Spring BootćØćć¹ć
ć¾ććÆćSpring Bootć«é¢ćććć¹ćć«ć¤ćć¦ćč¦ć¦ććć¾ćć
ćć”ćć®ććć„ć”ć³ććÆć4ć¤ć®ć»ćÆć·ć§ć³ć«åććć¦ćć¾ćć
- Core Features / Testing / Test Scope Dependencies
- ćć¹ćć®ć¹ć³ć¼ćć«å«ć¾ććć©ć¤ćć©ćŖ
- JUnit 5ćSpring TestćSpring Boot TestćAssertJćHamcrestćMockitoćJSONassertćJsonPath
- ćć¹ćć®ć¹ć³ć¼ćć«å«ć¾ććć©ć¤ćć©ćŖ
- Core Features / Testing / Testing Spring Applications
- Springć¢ććŖć±ć¼ć·ć§ć³ć®ćć¹ć
- Core Features / Testing / Testing Spring Boot Applications
- Spring Bootć¢ććŖć±ć¼ć·ć§ć³ć®ćć¹ć
- Core Features / Testing / Test Utilities
- ćć¹ćć«é¢ććć¦ć¼ćć£ćŖćć£
ę³Øē®ćć¦čŖćć¹ććÆćSpringć¢ććŖć±ć¼ć·ć§ć³ć®ćć¹ćć«é¢ćć話ćØćSpring Bootć¢ććŖć±ć¼ć·ć§ć³ć«é¢ćććć¹ćć®č©±ć§ććć
ćØććć§ćTest Scope Dependenciesć®Spring Testć®ē®ęć«ćÆć仄äøć®ććć«ęøććć¦ćć¾ćć
Spring Test & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
ćć¹ćć®ć¦ć¼ćć£ćŖćć£ćØćSpring Bootć¢ććŖć±ć¼ć·ć§ć³ć®ć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćććµćć¼ććććć®ć ććØęøććć¦ćć¾ććć
ć¾ćć Core Features / Testing / Testing Spring Applications ćč¦ććØć仄äøć®ććć«ęøććć¦ćć¾ćć
One of the major advantages of dependency injection is that it should make your code easier to unit test. You can instantiate objects by using the new operator without even involving Spring. You can also use mock objects instead of real dependencies.
Springćä½æćććnew
ę¼ē®åćä½æć£ćć¤ć³ć¹ćæć³ć¹åćććć¦ć¢ććÆćå©ēØćć¦åä½ćć¹ććć§ćććØććććØćęøććć¦ćć¾ćć
Springć®ApplicationContext
ćSpring TestćSpring Boot Testćä½æćć®ćÆć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć§ćććØćęøććć¦ćć¾ćć
Often, you need to move beyond unit testing and start integration testing (with a Spring ApplicationContext).
The Spring Framework includes a dedicated test module for such integration testing. You can declare a dependency directly to org.springframework:spring-test or use the spring-boot-starter-test āStarterā to pull it in transitively.
ć¤ć¾ććSpringć®ćć¹ćć«é¢ććę©č½ćÆćć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ććåÆ¾č±”ć«ćć¦ććććØć«ćŖćć¾ćć
Spring FrameworkćØćć¹ć
ććć§ćSpring Frameworkć®ćć¹ćć«é¢ććććć„ć”ć³ććč¦ć¦ćæć¾ćć
ćć®ććć„ć”ć³ććÆćäø»ć«ę¬”ć®2ć¤ć®å 容ć§ę§ęććć¦ćć¾ćć
ććć„ć”ć³ćć®åé ćč¦ććØććć®ćć¼ćøćÆåä½ćć¹ćć®ćć¹ććć©ćÆćć£ć¹ćØSpringć®ć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć«é¢ćććµćć¼ćć«
ć¤ćć¦ęøćć¦ććććØć®čØč¼ćććć¾ćć
This chapter covers Springās support for integration testing and best practices for unit testing.
ććć§ććSpring Frameworkć®ęä¾ććę©č½ć®å¤ććÆć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćåćć®ćć®ć ćØććććØć«ćŖćć¾ććć
Spring FrameworkćØåä½ćć¹ć
Spring Frameworkć®ćć¹ćć«é¢ćć話é”ćÆć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć«é¢ćććć®ć ćØććććØćÆćććć¾ććććåä½ćć¹ćć®ć»ćÆć·ć§ć³ć
ććć¾ćććććć”ććčæ½ć£ć¦ćæć¾ćććć
ćć®ć»ćÆć·ć§ć³ć®åé ćč¦ććØć仄äøć®ćććŖććØćęøććć¦ćć¾ćć
- POJOćÆćSpringććć®ä»ć®ć³ć³ćććä½æēØććć«
new
ę¼ē®åć§ć¤ć³ć¹ćæć³ć¹åććJUnitćŖć©ć§ćć¹ćć§ććććć«ćć¹ćć - ć³ć¼ćć®åé¢ćÆćć¢ććÆćä½æć£ć¦å®ē¾åÆč½
- SpringćÆćåä½ćć¹ćåćć®ć¢ććÆćŖććøć§ćÆććØćć¹ććµćć¼ććęä¾ćć
åŗę¬ēć«ćÆåä½ćć¹ććÆSpringćä½æććć«å®č”ć§ććć¹ćć§ćććć©ć³ćæć¤ć ć®ć»ććć¢ćććäøč¦ć«ćŖćć®ć§åä½ćć¹ććÆé«éć«
å®č”ćććććØćęå¾
ćć¦ćć¾ćć
True unit tests typically run extremely quickly, as there is no runtime infrastructure to set up. Emphasizing true unit tests as part of your development methodology can boost your productivity.
ćØćÆćććåä½ćć¹ćåćć®Springć®ćµćć¼ććććććØćć話ć®ććć§ćć
ęä¾ćććć®ćÆć仄äøć§ććć
- ć¢ććÆćŖććøć§ćÆć
- Environment
org.springframework.mock.env
ććć±ć¼ćøć«å«ć¾ććć¢ććÆå®č£MockEnvironment
ćMockPropertySource
- JNDI
org.springframework.mock.jndi
ććć±ć¼ćøć«å«ć¾ććć¢ććÆå®č£- ćć ćéęØå„Øåććć¦ćććSimple-JNDIēć§ä»£ęæććć
- Servlet API
org.springframework.mock.web
ććć±ć¼ćøć«å«ć¾ććć¢ććÆå®č£
- Spring Web Reactive
org.springframework.mock.http.server.reactive
ććć±ć¼ćøć«å«ć¾ććć¢ććÆå®č£
- Environment
- åä½ćć¹ććµćć¼ć
- General Testing Utilities
org.springframework.test.util
ććć±ć¼ćøć«å«ć¾ććć¦ć¼ćć£ćŖćć£- ćŖćć¬ćÆć·ć§ć³ć®ć¦ć¼ćć£ćŖćć£
ReflectionTestUtils
ćAOPé¢é£ć®ć¦ć¼ćć£ćŖćć£AopTestUtils
ćŖć©
- Spring MVC Testing Utilities
- General Testing Utilities
Spring FrameworkćØć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ć
Spring Frameworkć®ćć¹ćć«é¢ććććć„ć”ć³ćć«ćÆåä½ćć¹ććØć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć®å
容ćęøććć¦ććććć§ććć
ćć¼ćøå
ć®ććŖć„ć¼ć ćč¦ććØ大åćÆć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćåćć®ćć®ć§ććććØććććć¾ćć
ćć®ć»ćÆć·ć§ć³ć®ę¦č¦ćč¦ć¦ćæć¾ćć
Testing / Integration Testing / Overview
It is important to be able to perform some integration testing without requiring deployment to your application server or connecting to other enterprise infrastructure.
ć¢ććŖć±ć¼ć·ć§ć³ćµć¼ćć¼ćøć®ćććć¤ććä»ć®ćØć³ćæć¼ćć©ć¤ćŗć¤ć³ćć©ć¹ćć©ćÆćć£ć¼ćøć®ę„ē¶ćŖćć«ć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć
å®č”ć§ććććØćéč¦č¦ćć¦ćć¾ćć
ććć§ćäø»ć«Springć®ć³ć³ććć¹ććę£ććę§ēÆć§ćć¦ćććććć¼ćæćć¼ć¹ć¢ćÆć»ć¹ćŖć©ććć¹ćć§ććććØćć¦ćć¾ćć
ćć®ć»ćÆć·ć§ć³ć§ęøććć¦ććSpring Testļ¼spring-test
ļ¼ć®org.springframework.test
ććć±ć¼ćøć«ćÆćSpringć®ć³ć³ćććä½æć£ć
ć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć«é¢ćććÆć©ć¹ćå«ć¾ćć¦ćć¾ćć
ććććä½æēØćććć¹ććÆć¢ććŖć±ć¼ć·ć§ć³ćµć¼ćć¼ććć®ä»ć®ē°å¢ć«ä¾åćć¦ććććåä½ćć¹ććććÆé
ććć®ć®ćć¢ććŖć±ć¼ć·ć§ć³ćµć¼ćć¼ć«
ćććć¤ćć¦ć®Seleniumć§ć®ćć¹ćććŖć¢ć¼ććć¹ćććććć£ćØé«éć§ćć
Springć«ććććć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć®ē®ęØćÆ仄äøć«ęøććć¦ćć¾ćć
Testing / Integration Testing / Goals of Integration Testing
ćććć£ćęćć§ććć
- ćć¹ćéć®Springć³ć³ććć®ćć£ćć·ć„ē®”ē
- ćć¹ććć£ćÆć¹ćć£ć¤ć³ć¹ćæć³ć¹ć®DIć®ęä¾
- ēµåćć¹ćć«é©ćććć©ć³ć¶ćÆć·ć§ć³ē®”ēć®ęä¾
- ēµåćć¹ćć®ä½ęćå©ććććć®ćSpringåŗęć®ćć¼ć¹ćÆć©ć¹ć®ęä¾
ć¾ćć仄äøć®ę©č½ćå«ć¾ćć¦ćć¾ćć
- Testing / Integration Testing / JDBC Testing Support
org.springframework.test.jdbc
ććć±ć¼ćøć«å«ć¾ććJDBCé¢é£ć®ć¦ć¼ćć£ćŖćć£
- Testing / Integration Testing / Annotations
- Spring Testing Annotations
- TestContextćć¬ć¼ć ćÆć¼ćÆćØåććć¦ćåä½ćć¹ćććć³ć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćć§ä½æēØć§ććSpringåŗęć®ć¢ććć¼ć·ć§ć³ćęä¾
- Standard Annotation Support
- TestContextćć¬ć¼ć ćÆć¼ćÆć«ććJava EEļ¼Jakarta EEć«é¢ććć¢ććć¼ć·ć§ć³ćØćSpringć¢ććć¼ć·ć§ć³ć®ćµćć¼ć
- JUnit 4ććć³JUnit 5ćØēµćæåććä½æēØććć¢ććć¼ć·ć§ć³
- Spring Testing Annotations
- Spring TestContext Framework
org.springframework.test.context
ććć±ć¼ćøć«å«ć¾ćććć¹ćć«ćććSpringć®ć³ć³ććć¹ćć«é¢ććę©č½ćęä¾ćć
- WebTestClient
- ćµć¼ćć¼ć¢ććŖć±ć¼ć·ć§ć³ććć¹ćććććć«čØčØćććć
WebClient
ćć©ććććWebTestClient
ćęä¾ćć
- ćµć¼ćć¼ć¢ććŖć±ć¼ć·ć§ć³ććć¹ćććććć«čØčØćććć
- MockMvc
- Testing Client Applications
- å
éØēć«
RestTemplate
ćä½æēØććć¹ćæććä½æćććÆć©ć¤ć¢ć³ćå“ć§ć®ćć¹ćććµćć¼ććć
- å
éØēć«
Spring Bootć¢ććŖć±ć¼ć·ć§ć³ć®ćć¹ć
ćØććć§ćSpring Bootć®ćć¹ćć«é¢ććć»ćÆć·ć§ć³ć«ćÆćꬔć®2ć¤ćå«ć¾ćć¦ćć¾ććć
- Core Features / Testing / Testing Spring Applications
- Core Features / Testing / Testing Spring Boot Applications
Springć¢ććŖć±ć¼ć·ć§ć³ć®ćć¹ććÆćå ć»ć©ć¾ć§ęøćć¦ććSpring Testć®č©±ć§ćć
ć§ćÆSpring Bootć¢ććŖć±ć¼ć·ć§ć³ć®ćć¹ćć«é¢ćć話ćÆļ¼ćØćććØćSpring Bootć¢ććŖć±ć¼ć·ć§ć³ćSpringć¢ććŖć±ć¼ć·ć§ć³ć§ććććć
ē¹å„ćŖććØćÆćŖććØęøććć¦ćć¾ćć
A Spring Boot application is a Spring ApplicationContext, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context.
ć§ćććććć¤ćć®ę©č½ćÆęä¾ććććć§ćć仄äøć«äøéØćčØč¼ćć¾ćććåŗę¬ēć«ćÆAuto Configurationć«é¢ććę©č½ć®ććć§ććć
- Detecting Web Application Type
- Detecting Test Configuration
- Spring Bootć®
@*Test
ć¢ććć¼ć·ć§ć³ćä½æēØććććØć§ććć©ć¤ććŖć¼ć®ę§ęćęē¤ŗććŖćå “åćÆčŖåę§ęćč”ć
- Spring Bootć®
- Testing with a mock environment
- ć¢ććÆćµć¼ćć¼ć§ć®ćć¹ć
- Testing with a running server
- å®éć®ćµć¼ćć¼ćčµ·åćć¦ć®ćć¹ć
- Customizing WebTestClient
WebTestClient
ć®ć«ć¹ćæćć¤ćŗ
- Mocking and Spying Beans
- Beanć®ć¢ććÆććć³ć¹ćć¤
- Auto-configured Tests
- Spring Bootć®Auto Configurationćć«ć¹ćæćć¤ćŗććéØåēć«Auto Configurationćä½æćććć®å¤ę°ć®ć¹ć©ć¤ć¹ććć³ćć¹ć
- ä½æćę¹ćÆåć»ćÆć·ć§ć³ć«ęøććć¦ććććå„ćć¼ćøć®Test Auto-configuration Annotationsć«ćåć¹ć©ć¤ć¹ć«å«ć¾ććAuto ConfigurationćčØč¼ććć¦ćć
- Test Utilities
- ććć¤ćć®ćć¹ćć¦ć¼ćć£ćŖćć£ļ¼
ConfigDataApplicationContextInitializer
ćTestPropertyValues
ćOutputCapture
ćTestRestTemplate
- ććć¤ćć®ćć¹ćć¦ć¼ćć£ćŖćć£ļ¼
- Creating Your Own Auto-configuration
- ē¬čŖć®Auto Configurationć®ćµćć¼ć
ć¾ćØć
Springć®ćć¹ćć«é¢ććććć„ć”ć³ććč¦ć¦ćSpringć®ćć¹ćć®ćµćć¼ćć«åƾććčćę¹ććć«ćć¼ćććć§ć¼ćŗļ¼åä½ćć¹ćć
ć¤ć³ćć°ć¬ć¼ć·ć§ć³ćć¹ćļ¼ćć©ć®ćććŖę©č½ćęä¾ćććććć£ććēŗćć¦ćæć¾ććć
é°å²ę°ćÆćććććć£ćć®ć§ććć®ćć”ććć¤ćć®ę©č½ćä½æć£ć¦ćć¹ćć«é¢ććåå¼·ććć¦ććććć§ććć