EmailTokenGeneratorImpl implements EmailTokenGenerator { @Override @Transactional(readOnly = true) public String generateEmailToken() { short trialLimit = 3; return UniqueValueGenerator.generateUniqueValue(tokenGenerator::generateToken, this::isUnique, trialLimit); }  @Override @Transactional(readOnly = true) String generateEmailToken();  }
@Test public void testGenerateEmailToken() throws Exception { String value = "differentValueFirstTime"; when(tokenGenerator.generateToken()).thenReturn(value); when(userCreationRepository.findOneByEmailToken(value)).thenReturn(Optional.empty()); final String emailToken = sut.generateEmailToken(); assertEquals(value, emailToken); verify(tokenGenerator, times(1)).generateToken(); verify(userCreationRepository, times(1)).findOneByEmailToken(value); }
@Test public void testGenerateEmailToken_Throws_UniqueValueGeneratorException_When_Same_Value_Every_Time() throws Exception { String value = "sameValueEveryTime"; when(tokenGenerator.generateToken()).thenReturn(value); when(userCreationRepository.findOneByEmailToken(value)).thenReturn(Optional.of(mock(UserCreation.class))); thrown.expect(UniqueValueGeneratorException.class); sut.generateEmailToken(); verify(tokenGenerator, times(3)).generateToken(); verify(userCreationRepository, times(3)).findOneByEmailToken(value); }
@Test public void testGenerateEmailToken_Throws_UniqueValueGeneratorException_When_Non_Unique_Different_Value() throws Exception { String value1 = "differentValueEveryTime1"; String value2 = "differentValueEveryTime2"; String value3 = "differentValueEveryTime3"; when(tokenGenerator.generateToken()).thenReturn(value1).thenReturn(value2).thenReturn(value3); when(userCreationRepository.findOneByEmailToken(value1)).thenReturn(Optional.of(mock(UserCreation.class))); when(userCreationRepository.findOneByEmailToken(value2)).thenReturn(Optional.of(mock(UserCreation.class))); when(userCreationRepository.findOneByEmailToken(value3)).thenReturn(Optional.of(mock(UserCreation.class))); thrown.expect(UniqueValueGeneratorException.class); sut.generateEmailToken(); verify(tokenGenerator, times(3)).generateToken(); verify(userCreationRepository, times(1)).findOneByEmailToken(value1); verify(userCreationRepository, times(1)).findOneByEmailToken(value2); verify(userCreationRepository, times(1)).findOneByEmailToken(value3); }