Skip to content

Allahverdiyev-Rashad/spring-webflux-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sample java reactive programming project with:

  • Spring Webflux

  • Reactive Mongo

  • Junit4


Category entity:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document
public class Category {
    @Id
    private String id;
    private String description;
}

ReactiveMongoRepository :

public interface CategoryRepository extends ReactiveMongoRepository<Category, String> {
}

Sample get endpoint :

    @GetMapping("api/v1/categories/{id}")
    Mono<Category> findById(@PathVariable String id) {
        return categoryRepository.findById(id);
    }

Junit4 test :

public class CategoryControllerTest {

    WebTestClient webTestClient;
    CategoryRepository categoryRepository;
    CategoryController categoryController;

    @Before
    public void setUp() {
        categoryRepository = Mockito.mock(CategoryRepository.class);
        categoryController = new CategoryController(categoryRepository);
        webTestClient = WebTestClient.bindToController(categoryController).build();
    }
    
    @Test
    public void findById(){
        BDDMockito.given(categoryRepository.findById("someid"))
        .willReturn(Mono.just(Category.builder().description("Cat").build()));

        webTestClient.get()
        .uri("/api/v1/categories/someid")
        .exchange()
        .expectBody(Category.class);
        } 
}

This project does not contain service layer

About

Spring webflux rest api with reactive mongo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages