/** * This test method demonstrates how we can capture the method parameter with * an {@code ArgumentCaptor} object. */ @Test public void verifyThatMethodWasInvokedAndCaptureMethodParameterWithArgumentCaptorObject() { repository.findById(1L); ArgumentCaptor<Long> argument = ArgumentCaptor.forClass(Long.class); verify(repository).findById(argument.capture()); Long actualId = argument.getValue(); assertThat(actualId).isEqualByComparingTo(1L); }
/** * Verifies that the {@code deleteById()} method was invoked and the * correct id was passed as a method parameter. */ public void verify() { assertThat(deleteByIdCalled) .overridingErrorMessage("Expected that deleteById() was called but it was not") .isTrue(); assertThat(actualIdArgument) .overridingErrorMessage( "Invalid id argument. Expected: %d but was: %d", expectedIdArgument, actualIdArgument ) .isEqualByComparingTo(expectedIdArgument); } }
/** * This test demonstrates how we can access the actual method parameter by using * an argument captor. * * Note: We shouldn't verify interactions between system under test and * our test double if the method is a finder method. */ @Test public void verifyThatMethodWasInvokedByUsingArgumentCaptor() { repository.findById(1L); ArgumentCaptor<Long> longArgument = ArgumentCaptor.forClass(Long.class); verify(repository).findById(longArgument.capture()); Long actualId = longArgument.getValue(); assertThat(actualId).isEqualByComparingTo(1L); } }
/** * This test method demonstrates how we can configure the * returned value when the method in question takes no * method parameters. */ @Test public void shouldReturnValue() { //Won't invoke the real method doReturn(1L).when(repository).count(); //Will invoke the real method. //when(repository.count()).thenReturn(1L); long actualCount = repository.count(); assertThat(actualCount).isEqualByComparingTo(1L); }
public TaskAssert hasId(Long id) { assertThat(actual.getId()) .overridingErrorMessage("Expected id to be: %d but was: %d", id, actual.getId() ) .isEqualByComparingTo(id); return this; }
/** * This test method demonstrates how we can capture the method parameter with a lambda * expression. */ @Test public void verifyThatMethodWasInvokedAndCaptureMethodParameterWithLambdaExpression() { repository.findById(1L); verify(repository).findById(assertArg( argument -> assertThat(argument).isEqualByComparingTo(1L) )); } }
public TaskAssert wasCreatedBy(Long creatorId) { Creator actualCreator = actual.getCreator(); assertThat(actualCreator.getUserId()) .overridingErrorMessage("Expected that the task was created by user: %d but was created by user: %d", creatorId, actualCreator.getUserId() ) .isEqualByComparingTo(creatorId); return this; } }
@Test public void findById_ShouldThrowExceptionThatHasCorrectId() { Throwable thrown = catchThrowable(() -> finder.findById(ID)); NotFoundException notFound = (NotFoundException) thrown; assertThat(notFound.getId()).isEqualByComparingTo(ID); }
public TaskAssert isAssignedTo(Long assigneeId) { Assignee actualAssignee = actual.getAssignee(); assertThat(actualAssignee.getUserId()) .overridingErrorMessage("Expected that the task is assigned to user: %d but is assigned to user: %d", assigneeId, actualAssignee.getUserId() ) .isEqualByComparingTo(assigneeId); return this; }
/** * This test method demonstrates how we can configure the * returned value when the method in question takes no * method parameters. */ @Test public void shouldReturnValue() { given(repository.count()).willReturn(1L); long actualCount = repository.count(); assertThat(actualCount).isEqualByComparingTo(1L); }
final Long blockNumber) { assertThat(Hash.fromHexString(result.getString("hash"))).isEqualTo(transaction.hash()); assertThat(Long.decode(result.getString("nonce"))).isEqualByComparingTo(transaction.getNonce()); if (blockHash != null) { assertThat(Hash.fromHexString(result.getString("blockHash"))).isEqualTo(blockHash);
/** * This test method demonstrates how we can configure the * returned value when the method in question takes no * method parameters. */ @Test public void shouldReturnValue() { //Won't invoke the real method willReturn(1L).given(repository).count(); //Will invoke the real method. //given(repository.count()).willReturn(1L); long actualCount = repository.count(); assertThat(actualCount).isEqualByComparingTo(1L); }
@Test public void createCopyOf_WhenSourceTaskIsFound_ShouldReturnCreatedTask() { Task sourceTask = new TaskBuilder() .withDescription(SOURCE_TASK_DESCRIPTION) .withTitle(SOURCE_TASK_TITLE) .build(); when(repository.findById(SOURCE_TASK_ID)).thenReturn(Optional.of(sourceTask)); when(repository.save(isA(Task.class))).thenAnswer(new Answer<Task>() { @Override public Task answer(InvocationOnMock invocation) throws Throwable { Task parameter = (Task) invocation.getArguments()[0]; return new TaskBuilder() .withId(NEW_TASK_ID) .withCreator(parameter.getCreator().getUserId()) .withDescription(parameter.getDescription()) .withTitle(parameter.getTitle()) .build(); } }); LoggedInUser creator = new LoggedInUser(CREATOR_ID, CREATOR_USERNAME); Task created = service.createCopyOf(SOURCE_TASK_ID, creator); assertThat(created.getId()).isEqualByComparingTo(NEW_TASK_ID); assertThat(created.getCreator().getUserId()).isEqualByComparingTo(CREATOR_ID); assertThat(created.getDescription()).isEqualTo(SOURCE_TASK_DESCRIPTION); assertThat(created.getTitle()).isEqualTo(SOURCE_TASK_TITLE); } }
@Test public void createCopyOf_WhenSourceTaskIsFound_ShouldReturnCreatedTask() { Task sourceTask = new TaskBuilder() .withDescription(SOURCE_TASK_DESCRIPTION) .withTitle(SOURCE_TASK_TITLE) .build(); given(repository.findById(SOURCE_TASK_ID)).willReturn(Optional.of(sourceTask)); given(repository.save(isA(Task.class))).willAnswer(new Answer<Task>() { @Override public Task answer(InvocationOnMock invocation) throws Throwable { Task parameter = (Task) invocation.getArguments()[0]; return new TaskBuilder() .withId(NEW_TASK_ID) .withCreator(parameter.getCreator().getUserId()) .withDescription(parameter.getDescription()) .withTitle(parameter.getTitle()) .build(); } }); LoggedInUser creator = new LoggedInUser(CREATOR_ID, CREATOR_USERNAME); Task created = service.createCopyOf(SOURCE_TASK_ID, creator); assertThat(created.getId()).isEqualByComparingTo(NEW_TASK_ID); assertThat(created.getCreator().getUserId()).isEqualByComparingTo(CREATOR_ID); assertThat(created.getDescription()).isEqualTo(SOURCE_TASK_DESCRIPTION); assertThat(created.getTitle()).isEqualTo(SOURCE_TASK_TITLE); } }
assertThat(actualCount).isEqualByComparingTo(1L);