cucumber/cucumber-jvm — Issue #62: Problem when tests are outside a package
Author: lazee
Created: 2011-10-17 06:17:45+00:00
State: open

I know very well that this "bug" is not likely to be a problem for most people given that it is considered bad practice to have code outside packages. But still, it is allowed in Java (and I guess other dynamic languages on the jvm platform as well). I actually stumbled over this problem by accident when trying to reproduce a problem in one of the other issues here.

The problem is found in the method private List extraCodePaths(Class featureClass) (Line 152) in cucumber.junit.Cucumber.

If the test class is outside a package you will get "java.lang.StringIndexOutOfBoundsException: String index out of range: -1" because of the line:

String featurePackageName = featureClass.getName().substring(0, featureClass.getName().lastIndexOf("."));

In my case this leads to the code

String featurePackageName = featureClass.getName().substring(0, -1);

The quick fix for me was to just wrap the code with a check like this:

if (featureClass.getName().lastIndexOf(".") > -1) {
String featurePackageName = featureClass.getName().substring(0, featureClass.getName().lastIndexOf("."));
packageNamesOrScriptPaths.add(featurePackageName);
}

But I'm not sure of the consequences for the rest of the code by doing it this way.

Conclusion: Not a huge problem, but probably something worth fixing since it seems easy to fix.