### Question:
PatternService { public List<Pattern> findAllEntities() { return patternFactoryRegistry.getAllPatternFactories().stream() .map(PatternFactory::pattern).collect(Collectors.toList()); } @Autowired  PatternService(final PatternFactoryRegistry patternFactoryRegistry); List<Pattern> findAllEntities(); Optional<Pattern> findByIdentifier(final String identifier);  }### Answer:
@Test public void findAllEntities() throws Exception { final PatternFactoryRegistry registryMock = Mockito.mock(PatternFactoryRegistry.class); final PatternFactory patternFactoryMock = Mockito.mock(PatternFactory.class); final Pattern patternMock = Mockito.mock(Pattern.class); Mockito.doReturn(Optional.of(patternFactoryMock)).when(registryMock).getPatternFactoryForPackage("org.apache.fineract.cn.individuallending.api.v1"); Mockito.doReturn(Collections.singleton(patternFactoryMock)).when(registryMock).getAllPatternFactories(); Mockito.doReturn(patternMock).when(patternFactoryMock).pattern(); Mockito.doReturn("org.apache.fineract.cn.individuallending.api.v1").when(patternMock).getParameterPackage(); final PatternService testSubject = new PatternService(registryMock); final List<Pattern> all = testSubject.findAllEntities(); Assert.assertTrue(all.stream().anyMatch(pattern -> pattern.getParameterPackage().equals("org.apache.fineract.cn.individuallending.api.v1"))); final Optional<Pattern> pattern = testSubject.findByIdentifier("org.apache.fineract.cn.individuallending.api.v1"); Assert.assertTrue(pattern.isPresent()); }