Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Related #538.
  • Loading branch information
gregturn committed Mar 5, 2021
1 parent 38311c9 commit 61a623f
Showing 1 changed file with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.r2dbc.documentation;

import static org.mockito.Mockito.*;
import static org.springframework.data.domain.ExampleMatcher.*;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.*;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.endsWith;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.r2dbc.repository.R2dbcRepository;

/**
* Code to demonstrate Query By Example in reference documentation.
*
* @since 1.3
* @author Greg Turnquist
*/
public class QueryByExampleTests {

private EmployeeRepository repository;

@Test
void queryByExampleSimple() {

this.repository = mock(EmployeeRepository.class);

when(this.repository.findAll((Example<Employee>) any())) //
.thenReturn(Flux.just( //
new Employee(1, "Frodo", "ring bearer")));

// tag::example[]
Employee employee = new Employee(); // <1>
employee.setName("Frodo");
Expand All @@ -30,11 +60,23 @@ void queryByExampleSimple() {

// do whatever with the flux
// end::example[]

employees //
.as(StepVerifier::create) //
.expectNext(new Employee(1, "Frodo", "ring bearer")) //
.verifyComplete();
}

@Test
void queryByExampleCustomMatcher() {

this.repository = mock(EmployeeRepository.class);

when(this.repository.findAll((Example<Employee>) any())) //
.thenReturn(Flux.just( //
new Employee(1, "Frodo Baggins", "ring bearer"), //
new Employee(1, "Bilbo Baggins", "burglar")));

// tag::example-2[]
Employee employee = new Employee();
employee.setName("Baggins");
Expand All @@ -50,10 +92,17 @@ void queryByExampleCustomMatcher() {

// do whatever with the flux
// end::example-2[]

employees //
.as(StepVerifier::create) //
.expectNext(new Employee(1, "Frodo Baggins", "ring bearer")) //
.expectNext(new Employee(1, "Bilbo Baggins", "burglar")) //
.verifyComplete();
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

private @Id Integer id;
Expand Down

0 comments on commit 61a623f

Please sign in to comment.