-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests and code for a reactive sample
- Loading branch information
Showing
9 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.car; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class CarsApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(CarsApplication.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.car.domain; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Document | ||
public class Car { | ||
|
||
@Id | ||
private String id; | ||
|
||
private String name; | ||
|
||
private String type; | ||
|
||
public Car() { | ||
} | ||
|
||
public Car(String name, String type) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getType() { | ||
return this.type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
reactive/main/java/com/example/car/domain/CarRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.car.domain; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.data.repository.reactive.ReactiveCrudRepository; | ||
|
||
public interface CarRepository extends ReactiveCrudRepository<Car, String> { | ||
|
||
Mono<Car> findByName(String name); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.example.car.web; | ||
|
||
import com.example.car.domain.Car; | ||
import com.example.car.domain.CarRepository; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class CarController { | ||
|
||
private final CarRepository carRepository; | ||
|
||
public CarController(CarRepository carRepository) { | ||
this.carRepository = carRepository; | ||
} | ||
|
||
@GetMapping("/cars/{name}") | ||
public Mono<Car> getCar (@PathVariable String name) { | ||
return this.carRepository.findByName(name); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example</groupId> | ||
<artifactId>reactive-tdd-demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>reactive-tdd-demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.0.BUILD-SNAPSHOT</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-webflux</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>de.flapdoodle.embed</groupId> | ||
<artifactId>de.flapdoodle.embed.mongo</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.projectreactor</groupId> | ||
<artifactId>reactor-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spring-snapshots</id> | ||
<name>Spring Snapshots</name> | ||
<url>https://repo.spring.io/snapshot</url> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
</repository> | ||
<repository> | ||
<id>spring-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>https://repo.spring.io/milestone</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</repository> | ||
</repositories> | ||
|
||
<pluginRepositories> | ||
<pluginRepository> | ||
<id>spring-snapshots</id> | ||
<name>Spring Snapshots</name> | ||
<url>https://repo.spring.io/snapshot</url> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
</pluginRepository> | ||
<pluginRepository> | ||
<id>spring-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>https://repo.spring.io/milestone</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</pluginRepository> | ||
</pluginRepositories> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.example.car; | ||
|
||
import com.example.car.domain.Car; | ||
import com.example.car.domain.CarRepository; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.mongodb.core.CollectionOptions; | ||
import org.springframework.data.mongodb.core.ReactiveMongoOperations; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class IntegrationTests { | ||
|
||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@Autowired | ||
private CarRepository carRepository; | ||
|
||
@Autowired | ||
private ReactiveMongoOperations operations; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
this.operations | ||
.createCollection(Car.class, CollectionOptions.empty().size(1024 * 1024).maxDocuments( 100).capped()) | ||
.then() | ||
.block(); | ||
|
||
this.carRepository | ||
.save(new Car("prius", "hybrid")) | ||
.then() | ||
.block(); | ||
} | ||
|
||
@Test | ||
public void getCar_WithName_ReturnsCar() { | ||
Car car = this.webTestClient.get().uri("/cars/{name}", "prius") | ||
.exchange().expectStatus().isOk() | ||
.expectBody(Car.class).returnResult().getResponseBody(); | ||
assertThat(car.getName()).isEqualTo("prius"); | ||
assertThat(car.getType()).isEqualTo("hybrid"); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
reactive/test/java/com/example/car/domain/CarRepositoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.example.car.domain; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import reactor.test.StepVerifier; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(SpringRunner.class) | ||
@DataMongoTest | ||
public class CarRepositoryTests { | ||
|
||
@Autowired | ||
private CarRepository carRepository; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
this.carRepository.save(new Car("prius", "hybrid")) | ||
.then() | ||
.block(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
carRepository.deleteAll() | ||
.then() | ||
.block(); | ||
} | ||
|
||
@Test | ||
public void findByName_returnsCar() { | ||
StepVerifier.create(carRepository.findByName("prius")) | ||
.consumeNextWith(car -> { | ||
assertThat(car.getName()).isEqualTo("prius"); | ||
assertThat(car.getType()).isEqualTo("hybrid"); | ||
}) | ||
.verifyComplete(); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
reactive/test/java/com/example/car/web/CarControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.example.car.web; | ||
|
||
import com.example.car.domain.Car; | ||
import com.example.car.domain.CarRepository; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
|
||
@RunWith(SpringRunner.class) | ||
@WebFluxTest | ||
public class CarControllerTests { | ||
|
||
@MockBean | ||
private CarRepository carRepository; | ||
|
||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@Test | ||
public void getCar_WithName_returnsCar() throws Exception { | ||
Car car = new Car("prius", "hybrid"); | ||
given(carRepository.findByName("prius")).willReturn(Mono.just(car)); | ||
|
||
this.webTestClient.get().uri("/cars/{name}", "prius") | ||
.exchange().expectStatus().isOk() | ||
.expectBody() | ||
.jsonPath("name").isEqualTo("prius") | ||
.jsonPath("type").isEqualTo("hybrid"); | ||
} | ||
|
||
} |