Search in sources :

Example 6 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method shouldFailIfMatchedParametersAreNotFound.

@Test(expected = ParameterNotFound.class)
public void shouldFailIfMatchedParametersAreNotFound() {
    // Given
    SomeSteps stepsInstance = new SomeSteps();
    StepMatcher stepMatcher = mock(StepMatcher.class);
    MostUsefulConfiguration configuration = new MostUsefulConfiguration();
    InjectableStepsFactory stepsFactory = new InstanceStepsFactory(configuration, stepsInstance);
    StepCreator stepCreator = new StepCreator(stepsInstance.getClass(), stepsFactory, stepsContext, configuration.parameterConverters(), new ParameterControls(), stepMatcher, new SilentStepMonitor());
    // When
    when(stepMatcher.parameterNames()).thenReturn(new String[] {});
    stepCreator.matchedParameter("unknown");
// Then .. fail as expected
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) Test(org.junit.Test)

Example 7 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method shouldHandleObjectNotStoredFailure.

@Test
public void shouldHandleObjectNotStoredFailure() throws IntrospectionException {
    // Given
    setupContext();
    SomeSteps stepsInstance = new SomeSteps();
    InjectableStepsFactory stepsFactory = new InstanceStepsFactory(new MostUsefulConfiguration(), stepsInstance);
    StepMatcher stepMatcher = new RegexStepMatcher(StepType.WHEN, "I read from context", Pattern.compile("I read from context"), new String[] {});
    StepCreator stepCreator = new StepCreator(stepsInstance.getClass(), stepsFactory, stepsContext, null, new ParameterControls(), stepMatcher, new SilentStepMonitor());
    // When
    Method method = SomeSteps.methodFor("aMethodReadingFromContext");
    StepResult stepResult = stepCreator.createParametrisedStep(method, "When I read from context", "I read from context", new HashMap<String, String>()).perform(null);
    // Then
    assertThat(stepResult, instanceOf(Failed.class));
    Throwable cause = stepResult.getFailure().getCause();
    assertThat(cause, instanceOf(ObjectNotStoredException.class));
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) HashMap(java.util.HashMap) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) BeforeOrAfterFailed(org.jbehave.core.failures.BeforeOrAfterFailed) Failed(org.jbehave.core.steps.AbstractStepResult.Failed) Method(java.lang.reflect.Method) RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) ObjectNotStoredException(org.jbehave.core.steps.context.StepsContext.ObjectNotStoredException) Test(org.junit.Test)

Example 8 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method shouldMatchParametersByNamedAnnotationsIfConfiguredToNotUseDelimiterNamedParamters.

@SuppressWarnings("unchecked")
@Test
public void shouldMatchParametersByNamedAnnotationsIfConfiguredToNotUseDelimiterNamedParamters() throws Exception {
    // Given
    SomeSteps stepsInstance = new SomeSteps();
    parameterConverters = new ParameterConverters(new LoadFromClasspath(), new TableTransformers());
    StepMatcher stepMatcher = mock(StepMatcher.class);
    ParameterControls parameterControls = new ParameterControls().useDelimiterNamedParameters(false);
    StepCreator stepCreator = stepCreatorUsing(stepsInstance, stepMatcher, parameterControls);
    Map<String, String> params = new HashMap<>();
    params.put("theme", "a theme");
    params.put("variant", "a variant");
    when(stepMatcher.parameterNames()).thenReturn(params.keySet().toArray(new String[params.size()]));
    when(stepMatcher.parameter(1)).thenReturn("<t>");
    when(stepMatcher.parameter(2)).thenReturn("<v>");
    // When
    Step step = stepCreator.createParametrisedStep(SomeSteps.methodFor("aMethodWithANamedParameter"), "When I use parameters <t> and <v>", "I use parameters <t> and <v>", params);
    step.perform(null);
    // Then
    Map<String, String> results = (Map<String, String>) stepsInstance.args;
    assertThat(results.get("theme"), equalTo("a theme"));
    assertThat(results.get("variant"), equalTo("a variant"));
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) HashMap(java.util.HashMap) LoadFromClasspath(org.jbehave.core.io.LoadFromClasspath) Matchers.anyString(org.mockito.Matchers.anyString) ParametrisedStep(org.jbehave.core.steps.StepCreator.ParametrisedStep) HashMap(java.util.HashMap) Map(java.util.Map) TableTransformers(org.jbehave.core.model.TableTransformers) Test(org.junit.Test)

Aggregations

RegexStepMatcher (org.jbehave.core.parsers.RegexStepMatcher)8 StepMatcher (org.jbehave.core.parsers.StepMatcher)8 HashMap (java.util.HashMap)6 Matchers.anyString (org.mockito.Matchers.anyString)6 Test (org.junit.Test)5 MostUsefulConfiguration (org.jbehave.core.configuration.MostUsefulConfiguration)3 LoadFromClasspath (org.jbehave.core.io.LoadFromClasspath)3 TableTransformers (org.jbehave.core.model.TableTransformers)3 Successful (org.jbehave.core.steps.AbstractStepResult.Successful)3 ParametrisedStep (org.jbehave.core.steps.StepCreator.ParametrisedStep)3 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 BeforeOrAfterFailed (org.jbehave.core.failures.BeforeOrAfterFailed)1 Failed (org.jbehave.core.steps.AbstractStepResult.Failed)1 ObjectNotStoredException (org.jbehave.core.steps.context.StepsContext.ObjectNotStoredException)1