|
3 | 3 | import cucumber.resources.Consumer;
|
4 | 4 | import cucumber.resources.Resource;
|
5 | 5 | import cucumber.resources.Resources;
|
| 6 | +import cucumber.runtime.CucumberException; |
6 | 7 | import cucumber.runtime.FeatureBuilder;
|
7 | 8 | import cucumber.runtime.Runtime;
|
8 | 9 | import cucumber.runtime.model.CucumberFeature;
|
@@ -54,26 +55,39 @@ public void run() {
|
54 | 55 | /**
|
55 | 56 | * Constructor called by JUnit.
|
56 | 57 | */
|
57 |
| - public Cucumber(Class featureClass) throws InitializationError, IOException { |
58 |
| - super(featureClass, new ArrayList<Runner>()); |
| 58 | + public Cucumber(Class clazz) throws InitializationError, IOException { |
| 59 | + super(clazz, new ArrayList<Runner>()); |
| 60 | + assertNoDeclaredMethods(clazz); |
| 61 | + featurePath = featurePath(clazz); |
| 62 | + this.feature = parseFeature(featurePath, filters(clazz)); |
| 63 | + } |
59 | 64 |
|
60 |
| - featurePath = featurePath(featureClass); |
61 |
| - this.feature = parseFeature(featurePath, filters(featureClass)); |
| 65 | + private void assertNoDeclaredMethods(Class clazz) { |
| 66 | + if (clazz.getDeclaredMethods().length != 0) { |
| 67 | + throw new CucumberException( |
| 68 | + "\n\n" + |
| 69 | + "Classes annotated with @RunWith(Cucumber.class) must not define any methods.\n" + |
| 70 | + "Their sole purpose is to serve as an entry point for JUnit.\n" + |
| 71 | + "Step Definitions should be defined in their own classes.\n" + |
| 72 | + "This allows them to be reused across features.\n" + |
| 73 | + "Offending class: " + clazz + "\n" |
| 74 | + ); |
| 75 | + } |
62 | 76 | }
|
63 | 77 |
|
64 |
| - private String featurePath(Class featureClass) { |
65 |
| - cucumber.junit.Feature featureAnnotation = (cucumber.junit.Feature) featureClass.getAnnotation(cucumber.junit.Feature.class); |
| 78 | + private String featurePath(Class clazz) { |
| 79 | + cucumber.junit.Feature featureAnnotation = (cucumber.junit.Feature) clazz.getAnnotation(cucumber.junit.Feature.class); |
66 | 80 | String pathName;
|
67 | 81 | if (featureAnnotation != null) {
|
68 | 82 | pathName = featureAnnotation.value();
|
69 | 83 | } else {
|
70 |
| - pathName = featureClass.getName().replace('.', '/') + ".feature"; |
| 84 | + pathName = clazz.getName().replace('.', '/') + ".feature"; |
71 | 85 | }
|
72 | 86 | return pathName;
|
73 | 87 | }
|
74 | 88 |
|
75 |
| - private List<Object> filters(Class featureClass) { |
76 |
| - cucumber.junit.Feature featureAnnotation = (cucumber.junit.Feature) featureClass.getAnnotation(cucumber.junit.Feature.class); |
| 89 | + private List<Object> filters(Class clazz) { |
| 90 | + cucumber.junit.Feature featureAnnotation = (cucumber.junit.Feature) clazz.getAnnotation(cucumber.junit.Feature.class); |
77 | 91 | Object[] filters = new Object[0];
|
78 | 92 | if (featureAnnotation != null) {
|
79 | 93 | filters = toLong(featureAnnotation.lines());
|
@@ -142,14 +156,14 @@ private Long[] toLong(long[] primitiveLongs) {
|
142 | 156 | return longs;
|
143 | 157 | }
|
144 | 158 |
|
145 |
| - private static List<String> gluePaths(Class featureClass) { |
146 |
| - String className = featureClass.getName(); |
| 159 | + private static List<String> gluePaths(Class clazz) { |
| 160 | + String className = clazz.getName(); |
147 | 161 | String packageName = packageName(className);
|
148 | 162 | List<String> gluePaths = new ArrayList<String>();
|
149 | 163 | gluePaths.add(packageName);
|
150 | 164 |
|
151 | 165 | // Add additional ones
|
152 |
| - cucumber.junit.Feature featureAnnotation = (cucumber.junit.Feature) featureClass.getAnnotation(cucumber.junit.Feature.class); |
| 166 | + cucumber.junit.Feature featureAnnotation = (cucumber.junit.Feature) clazz.getAnnotation(cucumber.junit.Feature.class); |
153 | 167 | if (featureAnnotation != null) {
|
154 | 168 | gluePaths.addAll(asList(featureAnnotation.packages()));
|
155 | 169 | }
|
|
0 commit comments