cucumber.api.testng.TestNGCucumberRunner Maven / Gradle / Ivy
package cucumber.api.testng;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.CucumberException;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.RuntimeOptionsFactory;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;
import java.io.IOException;
/**
* Glue code for running Cucumber via TestNG.
*/
public class TestNGCucumberRunner {
private final cucumber.runtime.Runtime runtime;
/**
* Bootstrap the cucumber runtime
*
* @param clazz Which has the cucumber.api.CucumberOptions and org.testng.annotations.Test annotations
*/
public TestNGCucumberRunner(Class clazz) {
ClassLoader classLoader = clazz.getClassLoader();
ResourceLoader resourceLoader = new MultiLoader(classLoader);
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
TestNgReporter reporter = new TestNgReporter(System.out);
runtimeOptions.addPlugin(reporter);
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
runtime = new cucumber.runtime.Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
}
/**
* Run the Cucumber features
*/
public void runCukes() {
try {
runtime.run();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
if (!runtime.getErrors().isEmpty()) {
throw new CucumberException(runtime.getErrors().get(0));
} else if (runtime.exitStatus() != 0x00) {
throw new CucumberException("There are pending or undefined steps.");
}
}
}