44import org .junit .jupiter .api .Test ;
55
66import java .util .List ;
7+ import java .util .UUID ;
78
89import static org .assertj .core .api .Assertions .assertThat ;
10+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
11+ import static org .junit .jupiter .api .Assertions .assertThrows ;
12+ import static org .junit .jupiter .api .Assertions .assertTrue ;
913
1014class PersonServiceTest {
1115
@@ -22,7 +26,26 @@ void shouldCreatePerson() {
2226 Person person = personService .create (new Person (null , "Siva" , "siva@gmail.com" ));
2327 assertThat (person .getId ()).isNotNull ();
2428 assertThat (person .getName ()).isEqualTo ("Siva" );
25- assertThat (person .getEmail ()).isEqualTo ("siva@gmail.com" );
29+ assertThat (person .getEmail ()).isEqualTo ("siva@gmail.com" ).endsWith ("@gmail.com" );
30+ }
31+
32+ @ Test
33+ void shouldThrowExceptionWhenCreatePersonWithDuplicateEmail () {
34+ String email = UUID .randomUUID ().toString ()+"@gmail.com" ;
35+ personService .create (new Person (null , "Siva" , email ));
36+
37+ //Junit 5 assertion
38+ RuntimeException exception = assertThrows (RuntimeException .class , () -> {
39+ personService .create (new Person (null , "Siva" , email ));
40+ });
41+ assertTrue (exception .getMessage ()
42+ .contentEquals ("Person with email '" +email +"' already exists" ));
43+
44+ //Assertj assertion
45+ assertThatThrownBy (()-> {
46+ personService .create (new Person (null , "Siva" , email ));
47+ }).isInstanceOf (RuntimeException .class )
48+ .hasMessage ("Person with email '" +email +"' already exists" );
2649 }
2750
2851 @ Test
0 commit comments