3535import org .junit .Test ;
3636import org .mockito .ArgumentCaptor ;
3737import org .mockito .Mockito ;
38+ import org .mockito .invocation .InvocationOnMock ;
39+ import org .mockito .stubbing .Answer ;
40+ import org .springframework .test .util .ReflectionTestUtils ;
3841
3942import de .doubleslash .keeptime .common .DateProvider ;
4043import de .doubleslash .keeptime .model .Model ;
@@ -334,6 +337,14 @@ public void shouldCalculateSecondsCorrectlyWhenWorkItemsAreGiven() {
334337 @ Test
335338 public void shouldUpdateWorkItemPersistentlyWhenWorkItemIsEdited () {
336339 Mockito .when (mockedDateProvider .dateTimeNow ()).thenReturn (LocalDateTime .now ());
340+ Mockito .when (mockedWorkRepository .save (Mockito .any (Work .class ))).thenAnswer (new Answer <Work >() {
341+
342+ @ Override
343+ public Work answer (final InvocationOnMock invocation ) {
344+ final Object [] args = invocation .getArguments ();
345+ return (Work ) args [0 ];
346+ }
347+ });
337348
338349 final Project project1 = new Project ("workProject1" , "Some description" , Color .RED , true , 0 );
339350 model .getAllProjects ().add (project1 );
@@ -366,6 +377,13 @@ public void shouldUpdateWorkItemPersistentlyWhenWorkItemIsEdited() {
366377 @ Test
367378 public void shouldNotUpdateOthersWhenWorkItemIsEdited () {
368379 Mockito .when (mockedDateProvider .dateTimeNow ()).thenReturn (LocalDateTime .now ());
380+ Mockito .when (mockedWorkRepository .save (Mockito .any (Work .class ))).thenAnswer (new Answer <Work >() {
381+ @ Override
382+ public Work answer (final InvocationOnMock invocation ) {
383+ final Object [] args = invocation .getArguments ();
384+ return (Work ) args [0 ];
385+ }
386+ });
369387
370388 final Project project1 = new Project ("workProject1" , "Some description" , Color .RED , true , 0 );
371389 model .getAllProjects ().add (project1 );
@@ -375,14 +393,17 @@ public void shouldNotUpdateOthersWhenWorkItemIsEdited() {
375393
376394 final Work notToBeUpdatedWork = new Work (localDateNow , localDateTimeMorning .plusHours (0 ),
377395 localDateTimeMorning .plusHours (1 ), project1 , "originalWork" );
396+ ReflectionTestUtils .setField (notToBeUpdatedWork , "id" , 1 );
378397 model .getPastWorkItems ().add (notToBeUpdatedWork );
379398
380399 final Work originalWork = new Work (localDateNow , localDateTimeMorning .plusHours (1 ),
381400 localDateTimeMorning .plusHours (2 ), project1 , "originalWork" );
401+ ReflectionTestUtils .setField (originalWork , "id" , 2 );
382402 model .getPastWorkItems ().add (originalWork );
383403
384404 final Work newWork = new Work (localDateNow , localDateTimeMorning .plusHours (3 ), localDateTimeMorning .plusHours (4 ),
385405 project1 , "updated" );
406+ ReflectionTestUtils .setField (newWork , "id" , 3 );
386407
387408 testee .editWork (originalWork , newWork );
388409
0 commit comments