44
55import java .util .Arrays ;
66import java .util .List ;
7- import java .util .NoSuchElementException ;
8- import java .util .Optional ;
97
10- import org .baeldung .boot .boottest .Employee ;
11- import org .baeldung .boot .boottest .EmployeeRepository ;
12- import org .baeldung .boot .boottest .EmployeeService ;
13- import org .baeldung .boot .boottest .EmployeeServiceImpl ;
148import org .junit .Before ;
159import org .junit .Test ;
1610import org .junit .runner .RunWith ;
@@ -44,38 +38,38 @@ public void setUp() {
4438 Employee john = new Employee ("john" );
4539 john .setId (11L );
4640
47- Optional <Employee > emp = Optional .of (john );
48-
4941 Employee bob = new Employee ("bob" );
5042 Employee alex = new Employee ("alex" );
5143
5244 List <Employee > allEmployees = Arrays .asList (john , bob , alex );
5345
5446 Mockito .when (employeeRepository .findByName (john .getName ()))
55- .thenReturn (emp );
47+ .thenReturn (john );
48+ Mockito .when (employeeRepository .findByName (alex .getName ()))
49+ .thenReturn (alex );
5650 Mockito .when (employeeRepository .findByName ("wrong_name" ))
57- .thenReturn (Optional . empty () );
51+ .thenReturn (null );
5852 Mockito .when (employeeRepository .findById (john .getId ()))
59- .thenReturn (emp );
53+ .thenReturn (john );
6054 Mockito .when (employeeRepository .findAll ())
6155 .thenReturn (allEmployees );
6256 Mockito .when (employeeRepository .findById (-99L ))
63- .thenReturn (Optional . empty () );
57+ .thenReturn (null );
6458 }
6559
6660 @ Test
6761 public void whenValidName_thenEmployeeShouldBeFound () {
68- Optional < Employee > fromDb = employeeService . getEmployeeByName ( "john" ) ;
69- assertThat ( fromDb . get ()
70- . getName ()). isEqualTo ( "john" );
71-
72- verifyFindByNameIsCalledOnce ( "john" );
73- }
62+ String name = "alex" ;
63+ Employee found = employeeService . getEmployeeByName ( name );
64+
65+ assertThat ( found . getName ())
66+ . isEqualTo ( name );
67+ }
7468
75- @ Test ( expected = NoSuchElementException . class )
69+ @ Test
7670 public void whenInValidName_thenEmployeeShouldNotBeFound () {
77- Optional < Employee > fromDb = employeeService .getEmployeeByName ("wrong_name" );
78- fromDb . get ();
71+ Employee fromDb = employeeService .getEmployeeByName ("wrong_name" );
72+ assertThat ( fromDb ). isNull ();
7973
8074 verifyFindByNameIsCalledOnce ("wrong_name" );
8175 }
@@ -97,19 +91,18 @@ public void whenNonExistingName_thenEmployeeShouldNotExist() {
9791 }
9892
9993 @ Test
100- public void whenValidI_thendEmployeeShouldBeFound () {
101- Optional <Employee > fromDb = employeeService .getEmployeeById (11L );
102- assertThat (fromDb .get ()
103- .getName ()).isEqualTo ("john" );
94+ public void whenValidId_thenEmployeeShouldBeFound () {
95+ Employee fromDb = employeeService .getEmployeeById (11L );
96+ assertThat (fromDb .getName ()).isEqualTo ("john" );
10497
10598 verifyFindByIdIsCalledOnce ();
10699 }
107100
108- @ Test ( expected = NoSuchElementException . class )
101+ @ Test
109102 public void whenInValidId_thenEmployeeShouldNotBeFound () {
110- Optional < Employee > fromDb = employeeService .getEmployeeById (-99L );
103+ Employee fromDb = employeeService .getEmployeeById (-99L );
111104 verifyFindByIdIsCalledOnce ();
112- fromDb . get ();
105+ assertThat ( fromDb ). isNull ();
113106 }
114107
115108 @ Test
0 commit comments