parent
0c384f13b0
commit
322af68ae2
|
@ -2,7 +2,11 @@ package com.baeldung.passenger;
|
|||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
interface PassengerRepository extends JpaRepository<Passenger, Long>, CustomPassengerRepository {
|
||||
|
||||
Passenger findFirstByOrderBySeatNumberAsc();
|
||||
|
||||
List<Passenger> findByOrderBySeatNumberAsc();
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ import javax.persistence.EntityManager;
|
|||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@DataJpaTest
|
||||
|
@ -66,4 +68,30 @@ public class PassengerRepositoryIntegrationTest {
|
|||
Passenger actual = page.getContent().get(0);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSeveralPassengersWhenOrderedBySeatNumberAscThenThePassengersReturnedInCorrectOrder() {
|
||||
Passenger fred = Passenger.from("Fred", "Bloggs", 22);
|
||||
Passenger ricki = Passenger.from("Ricki", "Bobbie", 36);
|
||||
Passenger jill = Passenger.from("Jill", "Smith", 50);
|
||||
Passenger siya = Passenger.from("Siya", "Kolisi", 85);
|
||||
Passenger eve = Passenger.from("Eve", "Jackson", 95);
|
||||
|
||||
List<Passenger> passengers = repository.findByOrderBySeatNumberAsc();
|
||||
|
||||
assertThat(passengers, contains(fred, ricki, jill, siya, eve));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSeveralPassengersWhenFindAllWithSortBySeatNumberAscThenReturnPassengersInCorrectOrder() {
|
||||
Passenger fred = Passenger.from("Fred", "Bloggs", 22);
|
||||
Passenger ricki = Passenger.from("Ricki", "Bobbie", 36);
|
||||
Passenger jill = Passenger.from("Jill", "Smith", 50);
|
||||
Passenger siya = Passenger.from("Siya", "Kolisi", 85);
|
||||
Passenger eve = Passenger.from("Eve", "Jackson", 95);
|
||||
|
||||
List<Passenger> passengers = repository.findAll(Sort.by(Sort.Direction.ASC, "seatNumber"));
|
||||
|
||||
assertThat(passengers, contains(fred, ricki, jill, siya, eve));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue