Skip to content

Commit 5f0ebef

Browse files
author
liqiangqiang
committed
WebFlux 集成测试及部署
1 parent 0da9ac1 commit 5f0ebef

File tree

8 files changed

+287
-0
lines changed

8 files changed

+287
-0
lines changed

springboot-webflux-9-test/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-webflux-9-test</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-webflux-9-test :: Spring Boot WebFlux 集成测试及部署</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.0.1.RELEASE</version>
16+
</parent>
17+
18+
<dependencies>
19+
20+
<!-- Spring Boot Web Flux 依赖 -->
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-webflux</artifactId>
24+
</dependency>
25+
26+
<!-- ❤️Spring Boot 响应式 MongoDB 支持 -->
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
30+
</dependency>
31+
32+
<!-- Spring Boot Test 依赖 -->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>io.projectreactor</groupId>
41+
<artifactId>reactor-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<!-- Junit -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
</dependency>
51+
</dependencies>
52+
53+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
* Created by bysocket on 09/29/2017.
10+
*/
11+
// Spring Boot 应用的标识
12+
@SpringBootApplication
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
// 程序启动入口
17+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18+
SpringApplication.run(Application.class,args);
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.spring.springboot.dao;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface CityRepository extends ReactiveMongoRepository<City, Long> {
9+
10+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.spring.springboot.domain;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
/**
6+
* 城市实体类
7+
*
8+
*/
9+
public class City {
10+
11+
/**
12+
* 城市编号
13+
*/
14+
@Id
15+
private Long id;
16+
17+
/**
18+
* 省份编号
19+
*/
20+
private Long provinceId;
21+
22+
/**
23+
* 城市名称
24+
*/
25+
private String cityName;
26+
27+
/**
28+
* 描述
29+
*/
30+
private String description;
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public Long getProvinceId() {
41+
return provinceId;
42+
}
43+
44+
public void setProvinceId(Long provinceId) {
45+
this.provinceId = provinceId;
46+
}
47+
48+
public String getCityName() {
49+
return cityName;
50+
}
51+
52+
public void setCityName(String cityName) {
53+
this.cityName = cityName;
54+
}
55+
56+
public String getDescription() {
57+
return description;
58+
}
59+
60+
public void setDescription(String description) {
61+
this.description = description;
62+
}
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.spring.springboot.handler;
2+
3+
import org.spring.springboot.dao.CityRepository;
4+
import org.spring.springboot.domain.City;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Component;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
@Component
11+
public class CityHandler {
12+
13+
private final CityRepository cityRepository;
14+
15+
@Autowired
16+
public CityHandler(CityRepository cityRepository) {
17+
this.cityRepository = cityRepository;
18+
}
19+
20+
public Mono<City> save(City city) {
21+
return cityRepository.save(city);
22+
}
23+
24+
public Mono<City> findCityById(Long id) {
25+
26+
return cityRepository.findById(id);
27+
}
28+
29+
public Flux<City> findAllCity() {
30+
31+
return cityRepository.findAll();
32+
}
33+
34+
public Mono<City> modifyCity(City city) {
35+
36+
return cityRepository.save(city);
37+
}
38+
39+
public Mono<Long> deleteCity(Long id) {
40+
cityRepository.deleteById(id);
41+
return Mono.create(cityMonoSink -> cityMonoSink.success(id));
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.spring.springboot.webflux.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.handler.CityHandler;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
import reactor.core.publisher.Flux;
8+
import reactor.core.publisher.Mono;
9+
10+
@RestController
11+
@RequestMapping(value = "/city")
12+
public class CityWebFluxController {
13+
14+
@Autowired
15+
private CityHandler cityHandler;
16+
17+
@GetMapping(value = "/{id}")
18+
public Mono<City> findCityById(@PathVariable("id") Long id) {
19+
return cityHandler.findCityById(id);
20+
}
21+
22+
@GetMapping()
23+
public Flux<City> findAllCity() {
24+
return cityHandler.findAllCity();
25+
}
26+
27+
@PostMapping()
28+
public Mono<City> saveCity(@RequestBody City city) {
29+
return cityHandler.save(city);
30+
}
31+
32+
@PutMapping()
33+
public Mono<City> modifyCity(@RequestBody City city) {
34+
return cityHandler.modifyCity(city);
35+
}
36+
37+
@DeleteMapping(value = "/{id}")
38+
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
39+
return cityHandler.deleteCity(id);
40+
}
41+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring.data.mongodb.host=localhost
2+
spring.data.mongodb.database=admin
3+
spring.data.mongodb.port=27017
4+
spring.data.mongodb.username=admin
5+
spring.data.mongodb.password=admin
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.spring.springboot.handler;
2+
3+
import org.junit.Assert;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.spring.springboot.domain.City;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.web.reactive.server.WebTestClient;
13+
import org.springframework.web.reactive.function.BodyInserters;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
20+
public class CityHandlerTest {
21+
22+
@Autowired
23+
private WebTestClient webClient;
24+
25+
private static Map<String, City> cityMap = new HashMap<>();
26+
27+
@BeforeClass
28+
public static void setup() throws Exception {
29+
City wl = new City();
30+
wl.setId(1L);
31+
wl.setProvinceId(2L);
32+
wl.setCityName("WL");
33+
wl.setDescription("WL IS GOOD");
34+
cityMap.put("WL", wl);
35+
}
36+
37+
@Test
38+
public void testSave() throws Exception {
39+
40+
City expectCity = webClient.post().uri("/city")
41+
.contentType(MediaType.APPLICATION_JSON)
42+
.body(BodyInserters.fromObject(cityMap.get("WL")))
43+
.exchange()
44+
.expectStatus().isOk()
45+
.expectBody(City.class).returnResult().getResponseBody();
46+
47+
Assert.assertNotNull(expectCity);
48+
Assert.assertEquals(expectCity.getId(), cityMap.get("WL").getId());
49+
Assert.assertEquals(expectCity.getCityName(), cityMap.get("WL").getCityName());
50+
}
51+
52+
}

0 commit comments

Comments
 (0)