Skip to content

Commit 635bb45

Browse files
committed
Throw exception if @RunWith(Cucumber.class) classes have methods. Closes #77.
1 parent d349b4d commit 635bb45

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

junit/src/main/java/cucumber/junit/Cucumber.java

+26-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cucumber.resources.Consumer;
44
import cucumber.resources.Resource;
55
import cucumber.resources.Resources;
6+
import cucumber.runtime.CucumberException;
67
import cucumber.runtime.FeatureBuilder;
78
import cucumber.runtime.Runtime;
89
import cucumber.runtime.model.CucumberFeature;
@@ -54,26 +55,39 @@ public void run() {
5455
/**
5556
* Constructor called by JUnit.
5657
*/
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+
}
5964

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+
}
6276
}
6377

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);
6680
String pathName;
6781
if (featureAnnotation != null) {
6882
pathName = featureAnnotation.value();
6983
} else {
70-
pathName = featureClass.getName().replace('.', '/') + ".feature";
84+
pathName = clazz.getName().replace('.', '/') + ".feature";
7185
}
7286
return pathName;
7387
}
7488

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);
7791
Object[] filters = new Object[0];
7892
if (featureAnnotation != null) {
7993
filters = toLong(featureAnnotation.lines());
@@ -142,14 +156,14 @@ private Long[] toLong(long[] primitiveLongs) {
142156
return longs;
143157
}
144158

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();
147161
String packageName = packageName(className);
148162
List<String> gluePaths = new ArrayList<String>();
149163
gluePaths.add(packageName);
150164

151165
// 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);
153167
if (featureAnnotation != null) {
154168
gluePaths.addAll(asList(featureAnnotation.packages()));
155169
}

picocontainer/.cucumber/meta.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2+
"^the date is (.+)$": [
3+
"the date is 2011/10/25"
4+
],
5+
"^the date should be (.+)$": [
6+
"the date should be Oct 25 2011"
7+
],
28
"^I have (\\d+) (.*) in my belly$": [
39
"I have 12 cukes in my belly",
410
"I have 12 cukes in my belly"
511
],
612
"^there are (\\d+) cukes in my belly": [],
713
"^the (.*) contains (.*)": [],
814
"^I add (.*)": [],
9-
"^I should be (.*)$": [],
10-
"^the date is (.+)$": [
11-
"the date is 2011/10/25"
12-
],
13-
"^the date should be (.+)$": [
14-
"the date should be Oct 25 2011"
15-
]
15+
"^I should be (.*)$": []
1616
}

0 commit comments

Comments
 (0)