测试不使用springwebflux和reactivemongodb运行

jdg4fx2g  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(237)

我正在学习springboot,并且正在使用springwebflux和被动mongodb。我的控制器工作正常,但我的测试在空指针异常上失败。
我的身材是:

  1. plugins {
  2. id 'org.springframework.boot' version '2.4.2'
  3. id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  4. id 'java'
  5. }
  6. group = 'guru.springframework'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '11'
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
  14. implementation 'org.springframework.boot:spring-boot-starter-webflux'
  15. implementation 'junit:junit:4.12'
  16. compileOnly 'org.projectlombok:lombok:1.18.16'
  17. annotationProcessor 'org.projectlombok:lombok:1.18.16'
  18. testCompileOnly 'org.projectlombok:lombok:1.18.16'
  19. testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'
  20. //testCompile group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '3.0.0'
  21. testImplementation 'org.springframework.boot:spring-boot-starter-test'
  22. testImplementation 'io.projectreactor:reactor-test'
  23. }
  24. test {
  25. useJUnitPlatform()
  26. }

我的控制器是:
包guru.springframework.spring5webfluxrest.controllers;

  1. import guru.springframework.spring5webfluxrest.domain.Category;
  2. import guru.springframework.spring5webfluxrest.repositories.CategoryRepository;
  3. import org.reactivestreams.Publisher;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.web.bind.annotation.*;
  6. import reactor.core.publisher.Flux;
  7. import reactor.core.publisher.Mono;
  8. import java.util.concurrent.Flow;
  9. @RestController
  10. public class CategoryController {
  11. private final CategoryRepository categoryRepository;
  12. public CategoryController(CategoryRepository categoryRepository) {
  13. this.categoryRepository = categoryRepository;
  14. }
  15. @GetMapping("/api/v1/categories")
  16. public Flux<Category> list(){
  17. return categoryRepository.findAll();
  18. }
  19. @GetMapping("/api/v1/categories/{id}")
  20. public Mono<Category> getById(@PathVariable String id){
  21. return categoryRepository.findById(id);
  22. }
  23. @ResponseStatus(HttpStatus.CREATED)
  24. @PostMapping("/api/v1/categories")
  25. public Mono<Void> create(@RequestBody Publisher<Category> categoryStream){
  26. return categoryRepository.saveAll(categoryStream).then();
  27. }
  28. @PutMapping("/api/v1/categories/{id}")
  29. public Mono<Category> update (@PathVariable String id, @RequestBody Category category){
  30. category.setId(id);
  31. return categoryRepository.save(category);
  32. }
  33. @PatchMapping("/api/v1/categories/{id}")
  34. public Mono<Category> patch(@PathVariable String id, @RequestBody Category category){
  35. Category foundCategory = categoryRepository.findById(id).block();
  36. if(category.getDescription() != foundCategory.getDescription()){
  37. foundCategory.setDescription(category.getDescription());
  38. return categoryRepository.save(foundCategory);
  39. }
  40. return Mono.just(foundCategory);
  41. }
  42. }

我的测试是:

  1. package guru.springframework.spring5webfluxrest.controllers;
  2. import guru.springframework.spring5webfluxrest.domain.Category;
  3. import guru.springframework.spring5webfluxrest.repositories.CategoryRepository;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. //import org.junit.jupiter.api.Test;
  7. import org.mockito.BDDMockito;
  8. import org.mockito.Mockito;
  9. import org.reactivestreams.Publisher;
  10. import org.springframework.test.web.reactive.server.WebTestClient;
  11. import reactor.core.publisher.Flux;
  12. import reactor.core.publisher.Mono;
  13. import static org.mockito.ArgumentMatchers.any;
  14. import static org.mockito.ArgumentMatchers.anyString;
  15. import static org.mockito.BDDMockito.given;
  16. import static org.mockito.Mockito.never;
  17. import static org.mockito.Mockito.verify;
  18. public class CategoryControllerTest {
  19. WebTestClient webTestClient;
  20. CategoryRepository categoryRepository;
  21. CategoryController categoryController;
  22. @Before
  23. public void setUp() throws Exception {
  24. categoryRepository = Mockito.mock(CategoryRepository.class);
  25. categoryController = new CategoryController(categoryRepository);
  26. webTestClient = WebTestClient.bindToController(categoryController).build();
  27. }
  28. @Test
  29. public void list() {
  30. given(categoryRepository.findAll())
  31. .willReturn(Flux.just(Category.builder().description("Cat1").build(),
  32. Category.builder().description("Cat2").build()));
  33. webTestClient.get()
  34. .uri("/api/v1/categories/")
  35. .exchange()
  36. .expectBodyList(Category.class)
  37. .hasSize(2);
  38. }
  39. @Test
  40. public void getById() {
  41. given(categoryRepository.findById("someid"))
  42. .willReturn(Mono.just(Category.builder().description("Cat").build()));
  43. webTestClient.get()
  44. .uri("/api/v1/categories/someid")
  45. .exchange()
  46. .expectBody(Category.class);
  47. }
  48. @Test
  49. public void create() {
  50. given(categoryRepository.saveAll(any(Publisher.class)))
  51. .willReturn(Flux.just(Category.builder().description("descrp").build()));
  52. Mono<Category> catToSaveMono = Mono.just(Category.builder().description("Some Cat").build());
  53. webTestClient.post()
  54. .uri("/api/v1/categories")
  55. .body(catToSaveMono, Category.class)
  56. .exchange()
  57. .expectStatus()
  58. .isCreated();
  59. }
  60. @Test
  61. public void update() {
  62. given(categoryRepository.save(any(Category.class)))
  63. .willReturn(Mono.just(Category.builder().build()));
  64. Mono<Category> catToUpdateMono = Mono.just(Category.builder().description("Some Cat").build());
  65. webTestClient.put()
  66. .uri("/api/v1/categories/asdfjkl")
  67. .body(catToUpdateMono, Category.class)
  68. .exchange()
  69. .expectStatus()
  70. .isOk();
  71. }
  72. @Test
  73. public void testPatchNoChanges() {
  74. given(categoryRepository.findById(anyString()))
  75. .willReturn(Mono.just(Category.builder().build()));
  76. given(categoryRepository.save(any(Category.class)))
  77. .willReturn(Mono.just(Category.builder().build()));
  78. Mono<Category> catToUpdateMono = Mono.just(Category.builder().build());
  79. webTestClient.patch()
  80. .uri("/api/v1/categories/asdfasdf")
  81. .body(catToUpdateMono, Category.class)
  82. .exchange()
  83. .expectStatus()
  84. .isOk();
  85. verify(categoryRepository, never()).save(any());
  86. }
  87. }

任何帮助都将不胜感激。

ff29svar

ff29svar1#

您的测试用junit4的 @Test 但是您正在尝试用junit5运行它。junit5看不见。
取消注解 org.junit.jupiter.api.Test 导入和删除 import org.junit.Test (类别控制器测试)。
删除 implementation 'junit:junit:4.12'build.gradle 也。

相关问题