Skip to content

Commit 8ba2a50

Browse files
brasmussonmpkorstanje
authored andcommitted
[Spring] Deprecate context configuration by more than one class
1 parent 9a951ec commit 8ba2a50

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

spring/src/main/java/cucumber/runtime/java/spring/SpringFactory.java

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class SpringFactory implements ObjectFactory {
6262

6363
private final Collection<Class<?>> stepClasses = new HashSet<Class<?>>();
6464
private Class<?> stepClassWithSpringContext = null;
65+
private boolean deprecationWarningIssued = false;
6566

6667
public SpringFactory() {
6768
}
@@ -119,6 +120,14 @@ private static boolean hasComponentStereoType(Annotation annotation) {
119120

120121

121122
private void checkAnnotationsEqual(Class<?> stepClassWithSpringContext, Class<?> stepClass) {
123+
if (!deprecationWarningIssued) {
124+
deprecationWarningIssued = true;
125+
System.err.println(String.format("" +
126+
"WARNING: Having more than one glue class that configures " +
127+
"the spring context is deprecated. Found both %1$s and %2$s " +
128+
"that attempt to configure the spring context.",
129+
stepClass, stepClassWithSpringContext));
130+
}
122131
Annotation[] annotations1 = stepClassWithSpringContext.getAnnotations();
123132
Annotation[] annotations2 = stepClass.getAnnotations();
124133
if (annotations1.length != annotations2.length) {

spring/src/test/java/cucumber/runtime/java/spring/SpringFactoryTest.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import cucumber.runtime.java.spring.contexthierarchyconfig.WithDifferentContextHierarchyAnnotation;
1717
import cucumber.runtime.java.spring.dirtiescontextconfig.DirtiesContextBellyStepDefs;
1818
import cucumber.runtime.java.spring.metaconfig.dirties.DirtiesContextBellyMetaStepDefs;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.PrintStream;
1921
import org.junit.Rule;
2022
import org.junit.Test;
2123
import org.junit.rules.ExpectedException;
@@ -244,17 +246,37 @@ public void shouldUseCucumberXmlIfNoClassWithSpringAnnotationIsFound() {
244246
@Test
245247
public void shouldAllowClassesWithSameSpringAnnotations() {
246248
final ObjectFactory factory = new SpringFactory();
247-
factory.addClass(WithSpringAnnotations.class);
248-
factory.addClass(BellyStepdefs.class);
249+
PrintStream originalErr = System.err;
250+
try {
251+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
252+
System.setErr(new PrintStream(baos));
253+
factory.addClass(WithSpringAnnotations.class);
254+
factory.addClass(BellyStepdefs.class);
255+
assertEquals("WARNING: Having more than one glue class that configures " +
256+
"the spring context is deprecated. Found both class " +
257+
"cucumber.runtime.java.spring.contextconfig.BellyStepdefs and class " +
258+
"cucumber.runtime.java.spring.contextconfig.WithSpringAnnotations " +
259+
"that attempt to configure the spring context.\n",
260+
baos.toString());
261+
} finally {
262+
System.setErr(originalErr);
263+
}
249264
}
250265

251266
@Test
252267
public void shouldFailIfClassesWithDifferentSpringAnnotationsAreFound() {
253268
expectedException.expect(CucumberException.class);
254269
expectedException.expectMessage("Annotations differs on glue classes found: cucumber.runtime.java.spring.contexthierarchyconfig.WithContextHierarchyAnnotation, cucumber.runtime.java.spring.contexthierarchyconfig.WithDifferentContextHierarchyAnnotation");
255270
final ObjectFactory factory = new SpringFactory();
256-
factory.addClass(WithContextHierarchyAnnotation.class);
257-
factory.addClass(WithDifferentContextHierarchyAnnotation.class);
271+
PrintStream originalErr = System.err;
272+
try {
273+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
274+
System.setErr(new PrintStream(baos));
275+
factory.addClass(WithContextHierarchyAnnotation.class);
276+
factory.addClass(WithDifferentContextHierarchyAnnotation.class);
277+
} finally {
278+
System.setErr(originalErr);
279+
}
258280
}
259281

260282
@Test

0 commit comments

Comments
 (0)