久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

Spring Boot web項目的TDD流程

瀏覽:5日期:2023-07-13 09:34:35
目錄概述1 技術工具2 構建Spring Boot工程3 開始編寫測試和代碼1 Controller2 Service3 Repository4 總結概述

測試驅動開發(fā)可以分為三個周期,周而復始,紅燈-綠燈-重構。由以下幾個步驟構成:

編寫測試 運行所有測試 編寫代碼 運行所有測試 重構 運行所有測試

一開始編寫測試,肯定通不過,紅燈狀態(tài),進行代碼編寫,然后運行測試,測試通不過,測試通過,即變成綠燈。

測試不通過,或者需要重構代碼,再次運行所有測試代碼...

接下來通過一個簡單的,一個RESTful請求的Spring boot web項目,演示和說明TDD的過程。

這個功能大致是這樣的,一個simple元素有id和desc兩個屬性

用戶發(fā)送GET請求http接口 http://localhost:8080/simples 返回所有的simple元素的json數(shù)組

1 技術工具 JDK8+ Spring Boot 2.1+ maven or Gradle JPA JUnit 5+ Mockito Hamcrest

一個常見的RESTful請求處理的MVC架構:

用戶訪問http url 通過Controller層接口 Controller層調用Service的實現(xiàn) Service接口通過Repsoitory層訪問數(shù)據(jù)庫,并最終返回數(shù)據(jù)給用戶 2 構建Spring Boot工程

構建一個Spring Boot Maven工程,并添加所需的依賴

參考依賴如下

<properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions></dependency> </dependencies> <dependencyManagement><dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope> </dependency></dependencies> </dependencyManagement>3 開始編寫測試和代碼1 Controller

首先編寫測試Controller層的測試,test代碼區(qū)創(chuàng)建一個測試類,SimpleControllerTest

添加兩個注解 @ExtendWith和@WebMvcTest。

然后添加一個MockMvc對象,用來模擬mvc的請求。單元測試中,每個模塊應當獨立的測試,實際調用鏈中,Controller依賴Service層,因為當前測的是Controller層,對于Service層的代碼則進行mock,這可以使用一個注解

@MockBean

整個代碼如下

@ExtendWith({SpringExtension.class})@WebMvcTestpublic class SimpleControllerTest { @Autowired MockMvc mockMvc; @MockBean private SimpleService simpleService;}

SimpleService不存在,編譯不通過,紅燈,則創(chuàng)建它。

如是創(chuàng)建一個SimpleService作為Service層的Spring bean。

@Servicepublic class SimpleService {}

然后編寫請求/simples http請求的測試代碼

@Test void testFindAllSimples() throws Exception {List<Simple> simpleList = new ArrayList<>();simpleList.add(new Simple(1L,'one'));simpleList.add(new Simple(2L,'two'));when(simpleService.findAll()).thenReturn(simpleList);mockMvc.perform(MockMvcRequestBuilders.get('/simples').contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath('$', hasSize(2))).andDo(print()); }

when then結構來自Mockito框架,when表示了執(zhí)行的條件,then用于執(zhí)行驗證,這里的操作對simpleService.findAll方法結果進行了mock,這里 在這一層不需關心的simpleService的真實實現(xiàn)。后面perform方法 mock了 /simples的請求。

這里報錯,紅燈,接下來編寫Simple類的實現(xiàn)。

@Entitypublic class Simple { private Long id; private String desc;public Simple(String desc) {this.desc = desc; } }

因為simpleService.findAll方法未定義,所以還是報錯的,紅燈。接下來保持簡單,給SimpleService創(chuàng)建一個findAll方法。

public List<Simple> findAll() {return new ArrayList<>(); }

編譯問題都解決了,下面開始運行測試代碼。

報錯,

java.lang.AssertionError: No value at JSON path “$”

還是紅燈,這是因為我們mock的perform 沒有存在。接下來創(chuàng)建一個SimpleController類作為RestController,并編寫/simples請求的接口。

@RestControllerpublic class SimpleController { @Autowired private SimpleService simpleService; @GetMapping('/simples') public ResponseEntity<List<Simple>> getAllSimples() {return new ResponseEntity<>(simpleService.findAll(), HttpStatus.OK); }}

再次運行測試用例,測試都通過了,綠燈。

2 Service

接下來讓我們關注Service層的代碼測試,test代碼區(qū)創(chuàng)建一個SimpleServiceTest類。該類對下一層Repository依賴,同樣的,創(chuàng)建一個Repository的mock對象。

@SpringBootTestpublic class SimpleServiceTest { @MockBean private SimpleRepository simpleRepository;}

編譯報錯,紅燈,需要創(chuàng)建一個SimpleRepository。

@Repositorypublic interface SimpleRepository extends JpaRepository<Simple,Long> {}

以上,創(chuàng)建SimpleRepository作為實體Simple類對象的JPA存儲服務。

編寫測試代碼

@Test void testFindAll() {Simple simple = new Simple('one');simpleRepository.save(simple);SimpleService simpleService = new SimpleService(simpleRepository);List<Simple> simples = simpleService.findAll();Simple entity = simples.get(simples.size() - 1);assertEquals(simple.getDesc(),entity.getDesc());assertEquals(simple.getId(),entity.getId()); }

繼續(xù)解決編譯報錯的問題,SimpleService沒有構造方法。添加Repository 并注入bean。

@Servicepublic class SimpleService { private SimpleRepository simpleRepository; public SimpleService(SimpleRepository simpleRepository) {this.simpleRepository = simpleRepository; } public List<Simple> findAll() {return new ArrayList<>(); }}

這里插播一個題外話,為啥Spring推薦通過構造方法的方式注入bean, 方便編寫可測試代碼是個重要原因。

運行測試用例,會繼續(xù)報錯,這里是因為JPA hibernate沒有和實體類對象交互,需要添加主鍵注解,默認構造函數(shù) getter/setter 重新編寫實體類的代碼。

@Entitypublic class Simple { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String desc; public Simple() { } public Simple(String desc) {this.desc = desc; } // 省略 getter/setter ... }

修改完畢之后 運行測試用例 依然失敗,findAll方法測試未通過,修改SimpleService的findAll方法,調用 jpa repository的findAll方法

public List<Simple> findAll() {return simpleRepository.findAll(); }

現(xiàn)在再次運行測試用例,測試通過。

3 Repository

前面已經通過了TDD去實現(xiàn)Controller層和Service層的代碼,理論上Repository實現(xiàn)了JPA的接口,我們沒有做任何代碼的編寫,應該不需要進行測試,但是我們不確定數(shù)據(jù)是否通過數(shù)據(jù)庫進行了存儲和查詢。為了保證數(shù)據(jù)庫存儲,將真正的JPA respoitory實例注入的Service對象中。修改@MockBean 為@Autowired。

@SpringBootTestpublic class SimpleServiceTest { @Autowired private SimpleRepository simpleRepository; @Test void testFindAll() {Simple simple = new Simple('one');simpleRepository.save(simple);SimpleService simpleService = new SimpleService(simpleRepository);List<Simple> simpleEntities = simpleService.findAll();Simple entity = simpleEntities.get(simpleEntities.size() - 1);assertEquals(simple.getDesc(),entity.getDesc());assertEquals(simple.getId(),entity.getId()); }}

創(chuàng)建H2 database配置。

classpath下 創(chuàng)建schema.sql和data.sql,創(chuàng)建表和插入一點數(shù)據(jù)。

#************H2 Begin****************#創(chuàng)建表的MySql語句位置spring.datasource.schema=classpath:schema.sql#插入數(shù)據(jù)的MySql語句的位置spring.datasource.data=classpath:data.sql# 禁止自動根據(jù)entity創(chuàng)建表結構,表結構由schema.sql控制spring.jpa.hibernate.ddl-auto=nonespring.jpa.show-sql=true

schema.sql

DROP TABLE IF EXISTS simple;CREATE TABLE `simple` ( id BIGINT(20) auto_increment, desc varchar(255));

data.sql

INSERT INTO `simple`(`desc`) VALUES (’test1’);INSERT INTO `simple`(`desc`) VALUES (’test2’);

繼續(xù)運行測試用例,所有用例都測試通過,瀏覽器直接訪問localhost:8080/simples

返回data.sql插入的數(shù)據(jù)

[ {'id': 1,'desc': 'test1'},{'id': 2,'desc': 'test2'}]4 總結

以上是一個完整的TDD開發(fā)流程的演示,每一個模塊的測試具備獨立性,當前模塊中,可以mock其他模塊的數(shù)據(jù)。關于測試用例的結構,遵循的是AAA模式。

Arrange: 單元測試的第一步,需要進行必要的測試設置,譬如創(chuàng)建目標類對象,必要時,創(chuàng)建mock對象和其他變量初始化等等 Action: 調用要測試的目標方法 Assert: 單元測試的最后異步,檢查并驗證結果與預期的結果是否一致。

以上就是Spring Boot web項目的TDD流程的詳細內容,更多關于Spring Boot web項目TDD的資料請關注好吧啦網(wǎng)其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产精品久久久一区 | 亚洲精品美女久久久 | 激情视频网站 | 一区二区三区在线视频播放 | 国产一区2区 | 亚洲视频在线看 | 亚洲高清在线 | 91色在线观看 | 亚洲精品国产9999久久久久 | 欧美激情精品久久久久久免费 | 亚州成人 | 国产日韩精品视频 | 亚洲国产欧美在线 | 伊人电影综合网 | www.日韩在线观看 | 青青草久久久 | a视频在线 | 欧美在线一区二区 | 北条麻妃一区二区在线 | 成人国产免费视频 | 精品一区二区在线观看 | 久久精品视频免费看 | 热久久这里只有精品 | 国产精品美女久久久久aⅴ国产馆 | 狠狠躁天天躁夜夜添人人 | 中文字幕亚洲欧美日韩在线不卡 | 综合自拍偷拍 | 一区二区av在线 | 婷婷亚洲五月 | 91麻豆产精品久久久 | 欧美成年黄网站色视频 | 亚洲精品一区在线观看 | 午夜免费视频网站 | 国产精品国色综合久久 | 6080yy精品一区二区三区 | 精品久久一区二区 | 亚洲一区二区三区在线播放 | 国产精品99久久久久久宅男 | 精品国产不卡一区二区三区 | 欧美日韩精品久久久久 | 一级免费视频 |