From 94ca71320fde810cf08e589bea51d8bd7ee8c48f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4fer?= Date: Mon, 21 Dec 2015 16:09:14 +0100 Subject: [PATCH] reading of annotation not takes the executed test class into account and not the class where the test method is declared (fixes #171) --- CHANGELOG.md | 1 + gradle.properties | 2 +- .../com/tngtech/jgiven/impl/ScenarioBase.java | 4 ++-- .../tngtech/jgiven/impl/ScenarioExecutor.java | 2 +- .../jgiven/impl/ScenarioModelBuilder.java | 10 ++++----- .../impl/StandaloneScenarioExecutor.java | 4 ++-- .../impl/intercept/NoOpScenarioListener.java | 2 +- .../impl/intercept/ScenarioListener.java | 2 +- .../jgiven/impl/ConfigurationTest.java | 2 +- .../jgiven/junit/ScenarioExecutionRule.java | 9 ++------ ...tractScenarioForTestingTagInheritance.java | 22 +++++++++++++++++++ .../ClassImplementingAbstractScenario.java | 9 ++++++++ .../tngtech/jgiven/junit/tags/TestTag.java | 14 ++++++++++++ .../jgiven/testng/ScenarioTestListener.java | 2 +- 14 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/AbstractScenarioForTestingTagInheritance.java create mode 100644 jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/ClassImplementingAbstractScenario.java create mode 100644 jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/TestTag.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 05eb4cb069..1c3d88646e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ The `@Table` annotation to format step parameters as tables has been extended wi ## Fixed Issues +* Fixed the issue that tags of subclasses would not be visible on scenarios of superclasses [#171](https://github.com/TNG/JGiven/issues/171) * Fixed the issue that exceptions called within step methods are captured [#173](https://github.com/TNG/JGiven/issues/173) # v0.9.5 diff --git a/gradle.properties b/gradle.properties index 6230d72284..825e6cf249 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=0.10.0-SNAPSHOT +version=0.10.0 diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioBase.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioBase.java index 0ac1b6dd9f..adba2c27ef 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioBase.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioBase.java @@ -70,9 +70,9 @@ public void wireSteps( CanWire canWire ) { executor.wireSteps( canWire ); } - public ScenarioBase startScenario( Method method, List arguments ) { + public ScenarioBase startScenario( Class testClass, Method method, List arguments ) { performInitialization(); - executor.startScenario( method, arguments ); + executor.startScenario( testClass, method, arguments ); return this; } diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioExecutor.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioExecutor.java index a44d86d058..6f23a2cc51 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioExecutor.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/ScenarioExecutor.java @@ -56,7 +56,7 @@ public enum State { * @param method the method that started the scenario * @param arguments the test arguments with their parameter names */ - void startScenario( Method method, List arguments ); + void startScenario( Class testClass, Method method, List arguments ); void setListener( ScenarioListener listener ); 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 51acecbde0..8e266a872b 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 @@ -276,9 +276,9 @@ public void scenarioFailed( Throwable e ) { } @Override - public void scenarioStarted( Method method, List namedArguments ) { - readConfiguration( method.getDeclaringClass() ); - readAnnotations( method ); + public void scenarioStarted( Class testClass, Method method, List namedArguments ) { + readConfiguration( testClass ); + readAnnotations( testClass, method ); setParameterNames( getNames( namedArguments ) ); // must come at last @@ -328,7 +328,7 @@ private void readConfiguration( Class testClass ) { configuration = ConfigurationUtil.getConfiguration( testClass ); } - private void readAnnotations( Method method ) { + private void readAnnotations( Class testClass, Method method ) { String scenarioDescription = method.getName(); if( method.isAnnotationPresent( Description.class ) ) { @@ -348,7 +348,7 @@ private void readAnnotations( Method method ) { } if( scenarioCaseModel.getCaseNr() == 1 ) { - addTags( method.getDeclaringClass().getAnnotations() ); + addTags( testClass.getAnnotations() ); addTags( method.getAnnotations() ); } } diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java index 3eeeaf7d43..274044fdc0 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/StandaloneScenarioExecutor.java @@ -436,8 +436,8 @@ public void startScenario( String description ) { * @param arguments the test arguments with their parameter names */ @Override - public void startScenario( Method method, List arguments ) { - listener.scenarioStarted( method, arguments ); + public void startScenario( Class testClass, Method method, List arguments ) { + listener.scenarioStarted( testClass, method, arguments ); if( method.isAnnotationPresent( Pending.class ) ) { Pending annotation = method.getAnnotation( Pending.class ); diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/NoOpScenarioListener.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/NoOpScenarioListener.java index c75601ec15..cd61cf1e37 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/NoOpScenarioListener.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/NoOpScenarioListener.java @@ -16,7 +16,7 @@ public void scenarioFailed( Throwable e ) {} public void scenarioStarted( String string ) {} @Override - public void scenarioStarted( Method method, List arguments ) {} + public void scenarioStarted( Class testClass, Method method, List arguments ) {} @Override public void stepMethodInvoked( Method method, List arguments, InvocationMode mode, boolean hasNestedSteps ) {} diff --git a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/ScenarioListener.java b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/ScenarioListener.java index 42b49ef98c..5875ddb2b7 100644 --- a/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/ScenarioListener.java +++ b/jgiven-core/src/main/java/com/tngtech/jgiven/impl/intercept/ScenarioListener.java @@ -13,7 +13,7 @@ public interface ScenarioListener { void scenarioStarted( String string ); - void scenarioStarted( Method method, List arguments ); + void scenarioStarted( Class testClass, Method method, List arguments ); void stepMethodInvoked( Method method, List arguments, InvocationMode mode, boolean hasNestedSteps ); diff --git a/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ConfigurationTest.java b/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ConfigurationTest.java index 315ff55556..4c84f80a5d 100644 --- a/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ConfigurationTest.java +++ b/jgiven-core/src/test/java/com/tngtech/jgiven/impl/ConfigurationTest.java @@ -26,7 +26,7 @@ public class ConfigurationTest extends ScenarioTestBaseemptyList() ); given().some_step( new FooParam() ); given().another_step( new FooParam() ); diff --git a/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/ScenarioExecutionRule.java b/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/ScenarioExecutionRule.java index a7a35a4e84..0668512135 100644 --- a/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/ScenarioExecutionRule.java +++ b/jgiven-junit/src/main/java/com/tngtech/jgiven/junit/ScenarioExecutionRule.java @@ -8,12 +8,7 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.EnumSet; -import java.util.Iterator; -import java.util.List; +import java.util.*; import org.junit.internal.AssumptionViolatedException; import org.junit.rules.MethodRule; @@ -112,7 +107,7 @@ protected void starting( Statement base, FrameworkMethod testMethod, Object targ scenario.setModel( reportModel ); scenario.getExecutor().injectSteps( target ); - scenario.startScenario( testMethod.getMethod(), getNamedArguments( base, testMethod, target ) ); + scenario.startScenario( target.getClass(), testMethod.getMethod(), getNamedArguments( base, testMethod, target ) ); // inject state from the test itself scenario.getExecutor().readScenarioState( target ); diff --git a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/AbstractScenarioForTestingTagInheritance.java b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/AbstractScenarioForTestingTagInheritance.java new file mode 100644 index 0000000000..48d6755326 --- /dev/null +++ b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/AbstractScenarioForTestingTagInheritance.java @@ -0,0 +1,22 @@ +package com.tngtech.jgiven.junit.tags; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import com.tngtech.jgiven.junit.ScenarioTest; + +public abstract class AbstractScenarioForTestingTagInheritance extends ScenarioTest { + + @Test + public void ensure_all_tags_are_found() throws Throwable { + getScenario().finished(); + + List tagIds = getScenario().getModel().getLastScenarioModel().getTagIds(); + assertThat( tagIds ).containsAll( Arrays.asList( "TestTag" ) ); + + } +} diff --git a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/ClassImplementingAbstractScenario.java b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/ClassImplementingAbstractScenario.java new file mode 100644 index 0000000000..2cbfab8b1c --- /dev/null +++ b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/ClassImplementingAbstractScenario.java @@ -0,0 +1,9 @@ +package com.tngtech.jgiven.junit.tags; + +@TestTag +public class ClassImplementingAbstractScenario + extends + AbstractScenarioForTestingTagInheritance, ClassImplementingAbstractScenario.GivenStep, ClassImplementingAbstractScenario.GivenStep> { + + public static class GivenStep {} +} diff --git a/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/TestTag.java b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/TestTag.java new file mode 100644 index 0000000000..eaed72768b --- /dev/null +++ b/jgiven-junit/src/test/java/com/tngtech/jgiven/junit/tags/TestTag.java @@ -0,0 +1,14 @@ +package com.tngtech.jgiven.junit.tags; + +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import com.tngtech.jgiven.annotation.IsTag; + +@IsTag(name = "Security") +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface TestTag { + +} diff --git a/jgiven-testng/src/main/java/com/tngtech/jgiven/testng/ScenarioTestListener.java b/jgiven-testng/src/main/java/com/tngtech/jgiven/testng/ScenarioTestListener.java index cc11328e00..4d31c98bff 100644 --- a/jgiven-testng/src/main/java/com/tngtech/jgiven/testng/ScenarioTestListener.java +++ b/jgiven-testng/src/main/java/com/tngtech/jgiven/testng/ScenarioTestListener.java @@ -51,7 +51,7 @@ public void onTestStart( ITestResult paramITestResult ) { scenario.getExecutor().injectSteps( instance ); Method method = paramITestResult.getMethod().getConstructorOrMethod().getMethod(); - scenario.startScenario( method, getArgumentsFrom( method, paramITestResult ) ); + scenario.startScenario( instance.getClass(), method, getArgumentsFrom( method, paramITestResult ) ); // inject state from the test itself scenario.getExecutor().readScenarioState( instance );