BAEL-2406: Improve asserts in PassengerRepositoryIntegrationTest
This commit is contained in:
parent
e6a692f192
commit
630d07277e
|
@ -1,5 +1,17 @@
|
|||
package com.baeldung.passenger;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -12,16 +24,6 @@ import org.springframework.data.domain.PageRequest;
|
|||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@DataJpaTest
|
||||
@RunWith(SpringRunner.class)
|
||||
public class PassengerRepositoryIntegrationTest {
|
||||
|
@ -49,7 +51,7 @@ public class PassengerRepositoryIntegrationTest {
|
|||
assertEquals(1, passengers.size());
|
||||
|
||||
Passenger actual = passengers.get(0);
|
||||
assertEquals(actual, expected);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -58,7 +60,7 @@ public class PassengerRepositoryIntegrationTest {
|
|||
|
||||
Passenger actual = repository.findFirstByOrderBySeatNumberAsc();
|
||||
|
||||
assertEquals(actual, expected);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -68,7 +70,7 @@ public class PassengerRepositoryIntegrationTest {
|
|||
Page<Passenger> page = repository.findAll(PageRequest.of(0, 1,
|
||||
Sort.by(Sort.Direction.ASC, "seatNumber")));
|
||||
|
||||
assertEquals(page.getContent().size(), 1);
|
||||
assertEquals(1, page.getContent().size());
|
||||
|
||||
Passenger actual = page.getContent().get(0);
|
||||
assertEquals(expected, actual);
|
||||
|
@ -107,7 +109,7 @@ public class PassengerRepositoryIntegrationTest {
|
|||
Optional<Passenger> actual = repository.findOne(example);
|
||||
|
||||
assertTrue(actual.isPresent());
|
||||
assertEquals(actual.get(), Passenger.from("Fred", "Bloggs", 22));
|
||||
assertEquals(Passenger.from("Fred", "Bloggs", 22), actual.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -119,7 +121,7 @@ public class PassengerRepositoryIntegrationTest {
|
|||
Optional<Passenger> actual = repository.findOne(example);
|
||||
|
||||
assertTrue(actual.isPresent());
|
||||
assertEquals(actual.get(), Passenger.from("Fred", "Bloggs", 22));
|
||||
assertEquals(Passenger.from("Fred", "Bloggs", 22), actual.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -128,6 +130,7 @@ public class PassengerRepositoryIntegrationTest {
|
|||
Passenger eve = Passenger.from("Eve", "Jackson", 95);
|
||||
Passenger fred = Passenger.from("Fred", "Bloggs", 22);
|
||||
Passenger siya = Passenger.from("Siya", "Kolisi", 85);
|
||||
Passenger ricki = Passenger.from("Ricki", "Bobbie", 36);
|
||||
|
||||
ExampleMatcher customExampleMatcher = ExampleMatcher.matchingAny().withMatcher("firstName",
|
||||
ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase()).withMatcher("lastName",
|
||||
|
@ -139,21 +142,28 @@ public class PassengerRepositoryIntegrationTest {
|
|||
List<Passenger> passengers = repository.findAll(example);
|
||||
|
||||
assertThat(passengers, contains(jill, eve, fred, siya));
|
||||
assertThat(passengers, not(contains(ricki)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPassengers_whenFindByIgnoringMatcher_thenExpectedReturned() {
|
||||
Passenger fred = Passenger.from("Fred", "Bloggs", 22);
|
||||
Passenger ricki = Passenger.from("Ricki", "Bobbie", 36);
|
||||
|
||||
ExampleMatcher ignoringExampleMatcher = ExampleMatcher.matchingAny().withMatcher("lastName",
|
||||
ExampleMatcher.GenericPropertyMatchers.startsWith().ignoreCase()).withIgnorePaths("firstName", "seatNumber");
|
||||
|
||||
Example<Passenger> example = Example.of(Passenger.from(null, "b", null),
|
||||
ignoringExampleMatcher);
|
||||
|
||||
List<Passenger> passengers = repository.findAll(example);
|
||||
|
||||
assertThat(passengers, contains(fred, ricki));
|
||||
}
|
||||
@Test
|
||||
public void givenPassengers_whenFindByIgnoringMatcher_thenExpectedReturned() {
|
||||
Passenger jill = Passenger.from("Jill", "Smith", 50);
|
||||
Passenger eve = Passenger.from("Eve", "Jackson", 95);
|
||||
Passenger fred = Passenger.from("Fred", "Bloggs", 22);
|
||||
Passenger siya = Passenger.from("Siya", "Kolisi", 85);
|
||||
Passenger ricki = Passenger.from("Ricki", "Bobbie", 36);
|
||||
|
||||
ExampleMatcher ignoringExampleMatcher = ExampleMatcher.matchingAny().withMatcher("lastName",
|
||||
ExampleMatcher.GenericPropertyMatchers.startsWith().ignoreCase()).withIgnorePaths("firstName", "seatNumber");
|
||||
|
||||
Example<Passenger> example = Example.of(Passenger.from(null, "b", null),
|
||||
ignoringExampleMatcher);
|
||||
|
||||
List<Passenger> passengers = repository.findAll(example);
|
||||
|
||||
assertThat(passengers, contains(fred, ricki));
|
||||
assertThat(passengers, not(contains(jill)));
|
||||
assertThat(passengers, not(contains(eve)));
|
||||
assertThat(passengers, not(contains(siya)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue