diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java index 8b13b4af6e..de25deb869 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioModelBuilder.java @@ -210,9 +210,10 @@ private String getDescription( Method paramMethod ) { } As as = paramMethod.getAnnotation( As.class ); - return ReflectionUtil - .newInstance( as == null ? DefaultAsProvider.class : as.provider() ) - .as( as, paramMethod ); + AsProvider provider = as != null + ? ReflectionUtil.newInstance( as.provider() ) + : new DefaultAsProvider(); + return provider.as( as, paramMethod ); } public void setSuccess( boolean success ) { diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/params/DefaultAsProvider.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/params/DefaultAsProvider.java index 0a6aa3a5a7..9594913360 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/params/DefaultAsProvider.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/params/DefaultAsProvider.java @@ -11,6 +11,7 @@ /** * The default provider for a stage method, scenario or scenario class. * + * @since 0.12.0 */ public class DefaultAsProvider implements AsProvider { diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModel.java b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModel.java index e56e4bd6c3..a740971c6e 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModel.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/report/model/ReportModel.java @@ -12,6 +12,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.tngtech.jgiven.annotation.As; +import com.tngtech.jgiven.annotation.AsProvider; import com.tngtech.jgiven.annotation.Description; import com.tngtech.jgiven.impl.params.DefaultAsProvider; import com.tngtech.jgiven.impl.util.AssertionUtil; @@ -182,9 +183,10 @@ public synchronized void setTestClass( Class testClass ) { } As as = testClass.getAnnotation( As.class ); - name = ReflectionUtil - .newInstance( as == null ? DefaultAsProvider.class : as.provider() ) - .as( as, testClass ); + AsProvider provider = as != null + ? ReflectionUtil.newInstance( as.provider() ) + : new DefaultAsProvider(); + name = provider.as( as, testClass ); } public String getName() { diff --git a/jgiven-core/src/test/java/com/tngtech/jgiven/GivenTestStep.java b/jgiven-core/src/test/java/com/tngtech/jgiven/GivenTestStep.java index ad8f15ec71..883e430930 100644 --- a/jgiven-core/src/test/java/com/tngtech/jgiven/GivenTestStep.java +++ b/jgiven-core/src/test/java/com/tngtech/jgiven/GivenTestStep.java @@ -1,6 +1,9 @@ package com.tngtech.jgiven; +import java.lang.reflect.Method; + import com.tngtech.jgiven.annotation.As; +import com.tngtech.jgiven.annotation.AsProvider; import com.tngtech.jgiven.annotation.Format; import com.tngtech.jgiven.annotation.Formatf; import com.tngtech.jgiven.annotation.IntroWord; @@ -89,7 +92,12 @@ public GivenTestStep a_step_with_a_bracket_after_a_dollar( int value ) { return self(); } - public GivenTestStep a_step_with_a_printf_annotation_$( @Formatf( "%.2f" ) double d) { + @As( value = "output", provider = CustomAsProvider.class ) + public GivenTestStep a_step_with_an_As_annotation_and_a_custom_provider() { + return self(); + } + + public GivenTestStep a_step_with_a_printf_annotation_$( @Formatf( "%.2f" ) double d ) { return self(); } @@ -97,7 +105,7 @@ public GivenTestStep a_step_with_a_bracket_after_a_dollar( int value ) { return self(); } - public GivenTestStep a_step_with_a_boolean_$_parameter( @Format( value = BooleanFormatter.class, args = { "yes", "no" } ) boolean b) { + public GivenTestStep a_step_with_a_boolean_$_parameter( @Format( value = BooleanFormatter.class, args = { "yes", "no" } ) boolean b ) { return self(); } @@ -110,4 +118,19 @@ public GivenTestStep a_step_with_a_bracket_after_a_dollar( int value ) { public GivenTestStep an_intro_word_with_an_as_annotation() { return self(); } + + public static class CustomAsProvider implements AsProvider { + + @Override + public String as( As annotation, Method method ) { + return "Custom AsProvider " + annotation.value() + ": " + method.getName(); + } + + @Override + public String as( As annotation, Class scenarioClass ) { + return null; + } + + } + } diff --git a/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ScenarioModelBuilderTest.java b/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ScenarioModelBuilderTest.java index 562ad091eb..f04155c7b7 100644 --- a/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ScenarioModelBuilderTest.java +++ b/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ScenarioModelBuilderTest.java @@ -23,7 +23,12 @@ import com.tngtech.jgiven.annotation.DoNotIntercept; import com.tngtech.jgiven.annotation.IsTag; import com.tngtech.jgiven.base.ScenarioTestBase; -import com.tngtech.jgiven.report.model.*; +import com.tngtech.jgiven.report.model.ReportModel; +import com.tngtech.jgiven.report.model.ScenarioCaseModel; +import com.tngtech.jgiven.report.model.ScenarioModel; +import com.tngtech.jgiven.report.model.StepModel; +import com.tngtech.jgiven.report.model.Tag; +import com.tngtech.jgiven.report.model.Word; @RunWith( DataProviderRunner.class ) public class ScenarioModelBuilderTest extends ScenarioTestBase { @@ -309,6 +314,16 @@ public void characters_are_not_dropped_when_using_the_As_annotation() throws Thr assertThat( step.getCompleteSentence() ).isEqualTo( "Given a step with a bracket after a dollar 42 ]" ); } + @Test + public void a_custom_AsProvider_can_be_used() throws Throwable { + startScenario( "Scenario with a @As tag" ); + given().a_step_with_an_As_annotation_and_a_custom_provider(); + getScenario().finished(); + StepModel step = getScenario().getScenarioCaseModel().getFirstStep(); + assertThat( step.getCompleteSentence() ) + .isEqualTo( "Given Custom AsProvider output: a_step_with_an_As_annotation_and_a_custom_provider" ); + } + @Test public void camel_case_is_supported_in_steps() throws Throwable { startScenario( "Scenario camel case steps" );