DSTRN

RepositoryTest

Dec 14th, 2021 (edited)
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import static org.assertj.core.api.Assertions.assertThat;
  2.  
  3. @RunWith(SpringRunner.class)
  4. @DataJpaTest
  5. public class StudentRepositoryTest {
  6.  
  7.     @Autowired
  8.     private TestEntityManager testEntityManager;
  9.  
  10.     @Autowired
  11.     private StudentRepository studentRepository;
  12.  
  13.     private static final String STUDENT_ID = "sw25/2013";
  14.     private static final String STUDENT_FIRST_NAME = "Mina";
  15.     private static final String STUDENT_LAST_NAME = "Medic";
  16.  
  17.     @Test
  18.     public void shouldReturnEmptyOptionalWhenFindingNonExistingStudentByID() {
  19.         Optional<Student> student = studentRepository.findByIdentificationNumber(STUDENT_ID);
  20.  
  21.         assertFalse("Student is not present.", student.isPresent());
  22.     }
  23.  
  24.     @Test
  25.     public void shouldReturnStudentWhenFindingExistingStudentByID() {
  26.         Student student = new Student(null, STUDENT_FIRST_NAME, STUDENT_LAST_NAME, STUDENT_ID);
  27.         testEntityManager.persist(student);
  28.         testEntityManager.flush();
  29.  
  30.         Optional<Student> foundStudent = studentRepository.findByIdentificationNumber(STUDENT_ID);
  31.         assertThat(foundStudent.get()).isEqualToComparingFieldByField(student);
  32.        
  33.     }
  34. }
Add Comment
Please, Sign In to add comment