Repository navigation
Expand file tree
/
Copy pathBetweenTest.java
More file actions
57 lines (47 loc) · 1.84 KB
/
Copy pathBetweenTest.java
File metadata and controls
57 lines (47 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.github.wenhao.jpa.integration;
import com.github.wenhao.jpa.Specifications;
import com.github.wenhao.jpa.builder.PersonBuilder;
import com.github.wenhao.jpa.model.Person;
import com.github.wenhao.jpa.repository.PersonRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.domain.Range;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@DataJpaTest
public class BetweenTest {
@Autowired
private PersonRepository personRepository;
@Test
public void should_be_able_to_find_by_using_between() throws ParseException {
// given
Person jack = new PersonBuilder()
.name("Jack")
.birthday(getDate("1987-11-14"))
.build();
Person eric = new PersonBuilder()
.name("Eric")
.birthday(getDate("1990-10-12"))
.build();
personRepository.save(jack);
personRepository.save(eric);
// when
Specification<Person> specification = Specifications.<Person>builder()
.between(jack.getBirthday() != null, "birthday", new Range<Date>(getDate("1980-01-01"), getDate("1989-12-31")))
.build();
List<Person> persons = personRepository.findAll(specification);
// then
assertThat(persons.size()).isEqualTo(1);
}
private Date getDate(String source) throws ParseException {
return new SimpleDateFormat("yyyy-MM-dd").parse(source);
}
}