Search in sources :

Example 16 with CucumberBackendException

use of io.cucumber.core.backend.CucumberBackendException in project cucumber-jvm by cucumber.

the class Invoker method targetMethod.

private static Method targetMethod(Object target, Method method) {
    Class<?> targetClass = target.getClass();
    Class<?> declaringClass = method.getDeclaringClass();
    // same.
    if (targetClass.getClassLoader().equals(declaringClass.getClassLoader())) {
        return method;
    }
    try {
        // from interfaces are always public.
        if (Modifier.isPublic(method.getModifiers())) {
            return targetClass.getMethod(method.getName(), method.getParameterTypes());
        }
        // Loop through all the super classes until the declared method is
        // found.
        Class<?> currentClass = targetClass;
        while (currentClass != Object.class) {
            try {
                return currentClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
            } catch (NoSuchMethodException e) {
                currentClass = currentClass.getSuperclass();
            }
        }
        // The method does not exist in the class hierarchy.
        throw new NoSuchMethodException(String.valueOf(method));
    } catch (NoSuchMethodException e) {
        throw new CucumberBackendException("Could not find target method", e);
    }
}
Also used : CucumberBackendException(io.cucumber.core.backend.CucumberBackendException)

Example 17 with CucumberBackendException

use of io.cucumber.core.backend.CucumberBackendException in project cucumber-jvm by cucumber.

the class GuiceFactoryTest method shouldThrowExceptionIfTwoDifferentInjectorSourcesAreFound.

@Test
void shouldThrowExceptionIfTwoDifferentInjectorSourcesAreFound() {
    factory = new GuiceFactory();
    assertTrue(factory.addClass(YourInjectorSource.class));
    Executable testMethod = () -> factory.addClass(SecondInjectorSource.class);
    CucumberBackendException actualThrown = assertThrows(CucumberBackendException.class, testMethod);
    String exceptionMessage = String.format("" + "Glue class %1$s and %2$s are both implementing io.cucumber.guice.InjectorSource.\n" + "Please ensure only one class configures the Guice context\n" + "\n" + "By default Cucumber scans the entire classpath for context configuration.\n" + "You can restrict this by configuring the glue path.\n" + ClasspathSupport.configurationExamples(), SecondInjectorSource.class, YourInjectorSource.class);
    assertThat("Unexpected exception message", actualThrown.getMessage(), is(exceptionMessage));
}
Also used : YourInjectorSource(io.cucumber.guice.integration.YourInjectorSource) CucumberBackendException(io.cucumber.core.backend.CucumberBackendException) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Aggregations

CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)17 Test (org.junit.jupiter.api.Test)9 Executable (org.junit.jupiter.api.function.Executable)9 Argument (io.cucumber.core.stepexpression.Argument)4 ObjectFactory (io.cucumber.core.backend.ObjectFactory)3 StepDefinition (io.cucumber.core.backend.StepDefinition)3 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)3 CucumberException (io.cucumber.core.exception.CucumberException)3 Feature (io.cucumber.core.gherkin.Feature)3 Step (io.cucumber.core.gherkin.Step)3 Method (java.lang.reflect.Method)2 List (java.util.List)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Is.is (org.hamcrest.core.Is.is)2 Is.isA (org.hamcrest.core.Is.isA)2 IsEqual.equalTo (org.hamcrest.core.IsEqual.equalTo)2 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)2 CucumberInvocationTargetException (io.cucumber.core.backend.CucumberInvocationTargetException)1 ParameterInfo (io.cucumber.core.backend.ParameterInfo)1 CucumberExpressionException (io.cucumber.cucumberexpressions.CucumberExpressionException)1