Optional<Method> findMethod(String methodSpecPart, Class<?> clazz) { Matcher matcher = METHOD_PATTERN.matcher(methodSpecPart); Preconditions.condition(matcher.matches(), () -> String.format("Method [%s] does not match pattern [%s]", methodSpecPart, METHOD_PATTERN)); String methodName = matcher.group(1); String parameterTypeNames = matcher.group(2); return ReflectionUtils.findMethod(clazz, methodName, parameterTypeNames); }
private Method getMethod(Class<?> clazz, String methodName) { return ReflectionUtils.findMethod(clazz, methodName).orElseThrow(() -> new JUnitException( format("Could not find factory method [%s] in class [%s]", methodName, clazz.getName()))); }
protected Object executeMethod(String methodName, Class<?>[] parameterTypes, Object... arguments) { Method method = findMethod(this.target.getClass(), methodName, parameterTypes).orElseThrow( () -> new JUnitException(String.format("Failed to find method %s(%s) in class %s", methodName, ClassUtils.nullSafeToString(parameterTypes), this.target.getClass().getName()))); return invokeMethod(method, this.target, arguments); }
protected Object executeMethod(String methodName, Class<?>[] parameterTypes, Object... arguments) { Method method = findMethod(this.target.getClass(), methodName, parameterTypes).orElseThrow( () -> new JUnitException(String.format("Failed to find method %s(%s) in class %s", methodName, StringUtils.nullSafeToString(parameterTypes), this.target.getClass().getName()))); return invokeMethod(method, this.target, arguments); }
private void lazyLoadJavaMethod() { lazyLoadJavaClass(); if (this.javaMethod == null) { if (StringUtils.isNotBlank(this.methodParameterTypes)) { this.javaMethod = ReflectionUtils.findMethod(this.javaClass, this.methodName, this.methodParameterTypes).orElseThrow( () -> new PreconditionViolationException(String.format( "Could not find method with name [%s] and parameter types [%s] in class [%s].", this.methodName, this.methodParameterTypes, this.javaClass.getName()))); } else { this.javaMethod = ReflectionUtils.findMethod(this.javaClass, this.methodName).orElseThrow( () -> new PreconditionViolationException( String.format("Could not find method with name [%s] in class [%s].", this.methodName, this.javaClass.getName()))); } } }
@Override public void beforeAll(ExtensionContext extensionContext) { Map<String, String> newValues = extensionContext .getTestClass() .flatMap(c -> ReflectionUtils.findMethod(c, CONFIG_PROVIDER_METHOD)) .map(m -> { try { return (Stream<Pair<String, String>>) m.invoke(null); } catch (Exception e) { return Stream.empty(); } }) .orElse(Stream.empty()) .map(x -> (Pair<String, String>) x) .collect(Collectors.toMap(Pair::getFirst, Pair::getSecond)); EngineLocalConfig.getInstance(newValues); }
@Override public void beforeEach(ExtensionContext extensionContext) { origConfUtils = Config.getConfigUtils(); Config.setConfigUtils(mockConfigUtils); String configDescriptorMethodName = findAnnotation(extensionContext.getElement(), MockedConfig.class) .map(MockedConfig::value) .orElse(MockedConfig.DEFAULT_METHOD_NAME); extensionContext .getTestClass() .flatMap(c -> ReflectionUtils.findMethod(c, configDescriptorMethodName)) .map(m -> { try { return (Stream<MockConfigDescriptor>) m.invoke(null); } catch (Exception e) { return Stream.empty(); } }) .orElse(Stream.empty()) .map(MockConfigDescriptor.class::cast) .forEach(this::mockConfigValue); }