Source:
public void testCommandsConstructorsContract() { Package commandsFactoryPackage = CommandsFactory.class.getPackage(); Predicate<Constructor<?>> constructorRequiredByCommandsFactory = getConstructorRequiredByCommandsFactoryPredicate(); Predicate<Constructor<?>> constructorInaccessibleFromPackagePredicate = createConstructorInaccessibleFromPackagePredicate(commandsFactoryPackage); Map<Class<?>, List<Constructor<?>>> commandsWithInaccessibleConstructor = Arrays.stream(VdcActionType.values()) .filter(vdcActionType -> !vdcActionType.equals(VdcActionType.Unknown)) .map(vdcActionType -> CommandsFactory.getCommandClass(vdcActionType.toString())) .map(command -> Arrays.stream(command.getDeclaredConstructors())) .map(constructorStream -> constructorStream.filter(constructorRequiredByCommandsFactory)) .map(constructorStream -> constructorStream.filter(constructorInaccessibleFromPackagePredicate)) .flatMap(Function.identity()) .collect(Collectors.groupingBy(Constructor::getDeclaringClass)); assertThat("There are commands with at least one inaccessible constructor from CommandsFactory:", commandsWithInaccessibleConstructor.entrySet(), new BaseMatcher<Set<Map.Entry<Class<?>, List<Constructor<?>>>>>() { @SuppressWarnings("unchecked") @Override public boolean matches(Object o) { return ((Set<Map.Entry<Class<?>, List<Constructor<?>>>>) o).isEmpty(); } @Override public void describeTo(Description description) { description.appendText("All constructors should be accessible"); } @SuppressWarnings("unchecked") @Override public void describeMismatch(Object item, Description description) { description.appendText("Found inaccessible constructors:" + System.lineSeparator()); String startStr = ":" + System.lineSeparator() + "\t"; String separatorStr = System.lineSeparator() + "\t"; String endStr = System.lineSeparator() + System.lineSeparator(); ((Set<Map.Entry<Class<?>, List<Constructor<?>>>>) item).stream().forEach( commandsWithInaccessibleConstructors -> description.appendValueList( commandsWithInaccessibleConstructors.getKey().getSimpleName() + startStr, separatorStr, endStr, commandsWithInaccessibleConstructors.getValue()) ); } }); }

Target:
public void testCommandsConstructorsContract() { Package commandsFactoryPackage = CommandsFactory.class.getPackage(); Predicate<Constructor<?>> constructorRequiredByCommandsFactory = getConstructorRequiredByCommandsFactoryPredicate(); Predicate<Constructor<?>> constructorInaccessibleFromPackagePredicate = createConstructorInaccessibleFromPackagePredicate(commandsFactoryPackage); Map<Class<?>, List<Constructor<?>>> commandsWithInaccessibleConstructor = Arrays.stream(VdcActionType.values()) .filter(vdcActionType -> vdcActionType != VdcActionType.Unknown) .map(vdcActionType -> CommandsFactory.getCommandClass(vdcActionType.toString())) .map(command -> Arrays.stream(command.getDeclaredConstructors())) .map(constructorStream -> constructorStream.filter(constructorRequiredByCommandsFactory)) .map(constructorStream -> constructorStream.filter(constructorInaccessibleFromPackagePredicate)) .flatMap(Function.identity()) .collect(Collectors.groupingBy(Constructor::getDeclaringClass)); assertThat("There are commands with at least one inaccessible constructor from CommandsFactory:", commandsWithInaccessibleConstructor.entrySet(), new BaseMatcher<Set<Map.Entry<Class<?>, List<Constructor<?>>>>>() { @SuppressWarnings("unchecked") @Override public boolean matches(Object o) { return ((Set<Map.Entry<Class<?>, List<Constructor<?>>>>) o).isEmpty(); } @Override public void describeTo(Description description) { description.appendText("All constructors should be accessible"); } @SuppressWarnings("unchecked") @Override public void describeMismatch(Object item, Description description) { description.appendText("Found inaccessible constructors:" + System.lineSeparator()); String startStr = String.format(":%n\t"); String separatorStr = String.format("%n\t"); String endStr = String.format("%n%n"); ((Set<Map.Entry<Class<?>, List<Constructor<?>>>>) item).stream().forEach( commandsWithInaccessibleConstructors -> description.appendValueList( commandsWithInaccessibleConstructors.getKey().getSimpleName() + startStr, separatorStr, endStr, commandsWithInaccessibleConstructors.getValue()) ); } }); }