@Test public void findByMediaType() { // The 'protocol' parameter has to be case-insensitive. assertThat(find(parse("application/x-thrift; protocol=tbinary"))).containsSame(BINARY); assertThat(find(parse("application/x-thrift;protocol=TCompact"))).containsSame(COMPACT); assertThat(find(parse("application/x-thrift ; protocol=\"TjSoN\""))).containsSame(JSON); // An unknown parameter ('version' in this case) should not be accepted. assertThat(find(parse("application/x-thrift ; version=3;protocol=ttext"))).isEmpty(); // 'charset=utf-8' parameter should be accepted for TJSON and TTEXT. assertThat(find(parse("application/x-thrift; protocol=tjson; charset=utf-8"))).containsSame(JSON); assertThat(find(parse("application/vnd.apache.thrift.json; charset=utf-8"))).containsSame(JSON); assertThat(find(parse("application/x-thrift; protocol=ttext; charset=utf-8"))).containsSame(TEXT); assertThat(find(parse("application/vnd.apache.thrift.text; charset=utf-8"))).containsSame(TEXT); // .. but neither non-UTF-8 charsets: assertThat(find(parse("application/x-thrift; protocol=tjson; charset=us-ascii"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.json; charset=us-ascii"))).isEmpty(); assertThat(find(parse("application/x-thrift; protocol=ttext; charset=us-ascii"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.text; charset=us-ascii"))).isEmpty(); // .. nor binary/compact formats: assertThat(find(parse("application/x-thrift; protocol=tbinary; charset=utf-8"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.binary; charset=utf-8"))).isEmpty(); assertThat(find(parse("application/x-thrift; protocol=tcompact; charset=utf-8"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.compact; charset=utf-8"))).isEmpty(); }
@Test public void getLoggedInUser_present() { final Authentication expected = mock(Authentication.class); when(securityContextHolder.getContext().getAuthentication()).thenReturn(expected); final Optional<Authentication> loggedInUser = securityContextService.getLoggedInUser(); assertThat(loggedInUser).containsSame(expected); }
@Test public void getLoggedInUserProfile() { final Authentication authentication = mock(Authentication.class); final String userId = "fdsgzdg"; final Principal principal = mock(Principal.class); final UserProfile userProfile = mock(UserProfile.class); when(authentication.isAuthenticated()).thenReturn(true); when(authentication.getPrincipal()).thenReturn(principal); when(principal.getName()).thenReturn(userId); when(profileService.getUserProfile(principal)).thenReturn(userProfile); when(securityContextHolder.getContext().getAuthentication()).thenReturn(authentication); final Optional<UserProfile> result = securityContextService.getLoggedInUserProfile(); assertThat(result).containsSame(userProfile); }
@Test void findOne() { final long blockcheinEventId = 7546L; final BlockchainEvent blockchainEvent = mock(BlockchainEvent.class); final BlockchainEventDto blockchainEventDto = mock(BlockchainEventDto.class); when(blockchainEventRepo.findOne(blockcheinEventId)).thenReturn(Optional.of(blockchainEvent)); when(mapper.mapToOptional(same(blockchainEvent))).thenReturn(Optional.of(blockchainEventDto)); final Optional<BlockchainEventDto> result = service.findOne(blockcheinEventId); assertThat(result).containsSame(blockchainEventDto); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we verify the equality of actual and expected * method parameters by using an argument matcher. */ @Test public void shouldReturnObjectWhenExpectedParameterIsEqualToActualParameter() { Task expected = dummy(Task.class); //Won't invoke the real method doReturn(Optional.of(expected)).when(repository).findById(eq(1L)); //Will invoke the real method //when(repository.findById(eq(1L))).thenReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we can verify the equality of actual and expected * method parameters by using the {@code equals()} method. */ @Test public void shouldReturnObject() { Task expected = dummy(Task.class); //Won't invoke the real method doReturn(Optional.of(expected)).when(repository).findById(1L); //Will invoke the real method //when(repository.findById(1L)).thenReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test demonstrates how we can remove boilerplate code * and create returned {@code Answer} object by using a lambda * expression. */ @Test public void shouldReturnObjectByUsingAnswer() { Task expected = dummy(Task.class); when(repository.findById(1L)).thenAnswer(invocation -> { Long idParameter = (Long) invocation.getArguments()[0]; if (idParameter.equals(1L)) { return Optional.of(expected); } else { return Optional.empty(); } }); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); } }
@Test public void findClaim() { final long claimId = 697L; final Claim claim = Claim.builder().build(); final ClaimDto expected = ClaimDtoMother.aClaimDto().build(); when(claimRepository.findOne(claimId)).thenReturn(Optional.of(claim)); when(mappers.map(eq(Claim.class), eq(ClaimDto.class), same(claim))).thenReturn(expected); final Optional<ClaimDto> result = claimService.findOne(claimId); assertThat(result).containsSame(expected); }
@Test void findBy() { final String platformId = "dfsfd|FR|wfgwar|FR|243"; final GithubIssue githubIssue = GithubIssue.builder().build(); final PlatformIssueDto platformIssueDto = PlatformIssueDto.builder().build(); when(githubIssueService.findBy(platformId)).thenReturn(Optional.of(githubIssue)); when(mappers.map(GithubIssue.class, PlatformIssueDto.class, githubIssue)).thenReturn(platformIssueDto); final Optional<PlatformIssueDto> result = platformIssueService.findBy(Platform.GITHUB, platformId); assertThat(result).isPresent().containsSame(platformIssueDto); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we verify the equality of actual and expected * method parameters by using an argument matcher. */ @Test public void shouldReturnObjectWhenParameterIsAnyLong() { Task expected = dummy(Task.class); //Won't invoke the real method doReturn(Optional.of(expected)).when(repository).findById(anyLong()); //Will invoke the real method //when(repository.findById(anyLong())).thenReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This method demonstrates how we can configure the returned * answer by using an anonymous class. */ @Test public void shouldReturnObjectByUsingAnswerAndAnonymousClass() { Task expected = dummy(Task.class); when(repository.findById(1L)).thenAnswer(new Answer<Optional<Task>>() { @Override public Optional<Task> answer(InvocationOnMock invocation) throws Throwable { Long idParameter = (Long) invocation.getArguments()[0]; if (idParameter.equals(1L)) { return Optional.of(expected); } else { return Optional.empty(); } } }); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we verify the equality of actual and expected * method parameters by using an argument matcher. */ @Test public void shouldReturnObjectWhenExpectedParameterIsEqualToActualParameter() { Task expected = dummy(Task.class); given(repository.findById(eq(1L))).willReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we can verify the equality of actual and expected * method parameters by using the {@code equals()} method. */ @Test public void shouldReturnObject() { Task expected = dummy(Task.class); given(repository.findById(1L)).willReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test demonstrates how we can remove boilerplate code * and create returned {@code Answer} object by using a lambda * expression. */ @Test public void shouldReturnObjectByUsingAnswer() { Task expected = dummy(Task.class); given(repository.findById(1L)).willAnswer(invocation -> { Long idParameter = (Long) invocation.getArguments()[0]; if (idParameter.equals(1L)) { return Optional.of(expected); } else { return Optional.empty(); } }); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); } }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we verify the equality of actual and expected * method parameters by using an argument matcher. */ @Test public void shouldReturnObjectWhenExpectedParameterIsEqualToActualParameter() { Task expected = dummy(Task.class); //Won't invoke the real method willReturn(Optional.of(expected)).given(repository).findById(eq(1L)); //Will invoke the real method //given(repository.findById(eq(1L))).willReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we can verify the equality of actual and expected * method parameters by using the {@code equals()} method. */ @Test public void shouldReturnObject() { Task expected = dummy(Task.class); //Wont't invoke the real method willReturn(Optional.of(expected)).given(repository).findById(1L); //Will invoke the real method //given(repository.findById(1L)).willReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test method describes how we can configure the response * of a stubbed method by using an {@code Answer} object. */ @Test public void shouldReturnObjectByUsingAnswer() { Task expected = dummy(Task.class); given(repository.findById(1L)).willAnswer(new Answer<Optional<Task>>() { @Override public Optional<Task> answer(InvocationOnMock invocation) throws Throwable { Long idParameter = (Long) invocation.getArguments()[0]; if (idParameter.equals(1L)) { return Optional.of(expected); } else { return Optional.empty(); } } }); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we verify the equality of actual and expected * method parameters by using an argument matcher. */ @Test public void shouldReturnObjectWhenParameterIsAnyLong() { Task expected = dummy(Task.class); given(repository.findById(anyLong())).willReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This test method demonstrates how we can configure * the returned object when the method takes method parameters * and we verify the equality of actual and expected * method parameters by using an argument matcher. */ @Test public void shouldReturnObjectWhenParameterIsAnyLong() { Task expected = dummy(Task.class); //Won't invoke the real method willReturn(Optional.of(expected)).given(repository).findById(anyLong()); //Will invoke the real method. //given(repository.findById(anyLong())).willReturn(Optional.of(expected)); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }
/** * This method demonstrates how we can configure the returned * answer by using an anonymous class. */ @Test public void shouldReturnObjectByUsingAnswerAndAnonymousClass() { Task expected = dummy(Task.class); given(repository.findById(1L)).willAnswer(new Answer<Optional<Task>>() { @Override public Optional<Task> answer(InvocationOnMock invocation) throws Throwable { Long idParameter = (Long) invocation.getArguments()[0]; if (idParameter.equals(1L)) { return Optional.of(expected); } else { return Optional.empty(); } } }); Optional<Task> actual = repository.findById(1L); assertThat(actual).containsSame(expected); }