MrnServiceImpl implements MrnService { @Override public String generateMrn() { final short iterationLimit = 3; return UniqueValueGenerator .generateUniqueValue(this::generateRandomMrn, generatedValue -> patientRepository .findAllByMedicalRecordNumber(generatedValue) .size() == 0, iterationLimit); } MrnServiceImpl(String prefix, PatientRepository patientRepository); @Override String generateMrn();  }
@Test public void testGenerateMrn() { final String prefixMock = "PREFIXMOCK"; ReflectionTestUtils.setField(sut, "prefix", prefixMock); when(patientRepository.findAllByMedicalRecordNumber(anyString())) .thenReturn(new ArrayList<Patient>(1)); String mrn = sut.generateMrn(); assertTrue(StringUtils.hasText(mrn)); assertTrue(mrn.startsWith(prefixMock)); }
@Test public void testGenerateMrn_Throws_UniqueValueGeneratorException() { final String prefixMock = "PREFIXMOCK"; ReflectionTestUtils.setField(sut, "prefix", prefixMock); Patient patientMock = mock(Patient.class); when(patientRepository.findAllByMedicalRecordNumber(anyString())) .thenReturn(Arrays.asList(patientMock)); thrown.expect(UniqueValueGeneratorException.class); sut.generateMrn(); }