黄瓜:从Spring Boot jar运行时找不到后端

问题描述 投票:4回答:5

我正在创建一个小型测试框架,该框架应同时使用Cucumber和Spring Boot平台。想法是让整个应用程序打包为单个jar,并在对BDD功能进行适当参数化后运行。

框架在命令行运行器模式下启动,如下所示:

public class FwApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(FwApplication.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        JUnitCore.main(CucumberIntegration.class.getCanonicalName());
    }
}

然后有CucumberIntegration类:

@RunWith(Cucumber.class)
@CucumberOptions(features = "config/features")
@ContextConfiguration(classes= AppConfiguration.class)
public class CucumberIntegration {
}

我也有一些简单的测试,这些测试可以在我的IDE上正常运行,但是当我尝试打包应用程序并通过java -jar fw-0.0.1-SNAPSHOT.jar运行它时,会看到以下内容:

There was 1 failure:
1) initializationError(com.fmr.bddfw.test.CucumberIntegration)
cucumber.runtime.CucumberException: No backends were found. Please make sure you have a backend module on your CLASSPATH.
        at cucumber.runtime.Runtime.<init>(Runtime.java:81)
        at cucumber.runtime.Runtime.<init>(Runtime.java:70)
        (...)

所有必需的jar已在maven创建的一个jar中,并且在我的IDE下可以正常工作。

任何想法有什么帮助?

EDIT:这是我的pom文件。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xxx</groupId>
    <artifactId>fw</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>fw</name>
    <description>BDD Testing Framework</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <cucumber.version>1.2.5</cucumber.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
maven spring-boot cucumber-java
5个回答
3
投票

使用Marcus给出的建议:

Step1:创建您的自定义MultiLoader类:

package cucumber.runtime.io;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class CustomMultiLoader implements ResourceLoader {
    public static final String CLASSPATH_SCHEME = "classpath*:";
    public static final String CLASSPATH_SCHEME_TO_REPLACE = "classpath:";
    private final ClasspathResourceLoader classpath;
    private final FileResourceLoader fs;
    public CustomMultiLoader(ClassLoader classLoader) {
        classpath = new ClasspathResourceLoader(classLoader);
        fs = new FileResourceLoader();
    }
    @Override
    public Iterable<Resource> resources(String path, String suffix) {
        if (isClasspathPath(path)) {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            String locationPattern = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME) + "/**/*" + suffix;
            org.springframework.core.io.Resource[] resources;
            try {
                resources = resolver.getResources(locationPattern);
            } catch (IOException e) {
                resources = null;
                e.printStackTrace();
            }
            return convertToCucumberIterator(resources);
        } else {
            return fs.resources(path, suffix);
        }
    }
    private Iterable<Resource> convertToCucumberIterator(org.springframework.core.io.Resource[] resources) {
        List<Resource> results = new ArrayList<Resource>();
        for (org.springframework.core.io.Resource resource : resources) {
            results.add(new ResourceAdapter(resource));
        }
        return results;
    }

    public static String packageName(String gluePath) {
        if (isClasspathPath(gluePath)) {
            gluePath = stripClasspathPrefix(gluePath);
        }
        return gluePath.replace('/', '.').replace('\\', '.');
    }

    private static boolean isClasspathPath(String path) {
        if (path.startsWith(CLASSPATH_SCHEME_TO_REPLACE)) {
            path = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME);
        }
        return path.startsWith(CLASSPATH_SCHEME);
    }

    private static String stripClasspathPrefix(String path) {
        if (path.startsWith(CLASSPATH_SCHEME_TO_REPLACE)) {
            path = path.replace(CLASSPATH_SCHEME_TO_REPLACE, CLASSPATH_SCHEME);
        }
        return path.substring(CLASSPATH_SCHEME.length());
    }

}

Step2:在org.springframework.core.io.Resource和Cucumber.runtime.io.Resource之间创建一个适配器:

package cucumber.runtime.io;

import java.io.IOException;
import java.io.InputStream;

public class ResourceAdapter implements Resource {
    org.springframework.core.io.Resource springResource;

    public ResourceAdapter(org.springframework.core.io.Resource springResource) {
        this.springResource = springResource;
    }

    public String getPath() {
        try {
            return springResource.getFile().getPath();
        } catch (IOException e) {
            try {
                return springResource.getURL().toString();
            } catch (IOException e1) {
                e1.printStackTrace();
                return "";
            }
        }
    }

    public String getAbsolutePath() {
        try {
            return springResource.getFile().getAbsolutePath();
        } catch (IOException e) {
            return null;
        }
    }

    public InputStream getInputStream() throws IOException {
        return this.springResource.getInputStream();
    }

    public String getClassName(String extension) {

        String path = this.getPath();
        if (path.startsWith("jar:")) {
            path = path.substring(path.lastIndexOf("!") + 2);
            return path.substring(0, path.length() - extension.length()).replace('/', '.');
        } else {
            path = path.substring(path.lastIndexOf("classes") + 8);
            return path.substring(0, path.length() - extension.length()).replace('\\', '.');
        }

    }
}

Step3:创建使用CustomMultiLoader的自定义主类:

package cucumber.runtime.io;

import static java.util.Arrays.asList;

import java.io.IOException;
import java.util.ArrayList;

import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;

public class CucumberStaticRunner {

    public static void startTests(String[] argv) throws Throwable {
        byte exitstatus = run(argv, Thread.currentThread().getContextClassLoader());
        System.exit(exitstatus);
    }

    public static byte run(String[] argv, ClassLoader classLoader) throws IOException {
        RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));

        ResourceLoader resourceLoader = new CustomMultiLoader(classLoader);
        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
        runtime.run();
        return runtime.exitStatus();
    }
}

Step4:调用自定义的主类,而不是cucumber.api.cli.Main.main:

String[] cucumberOptions = { "--glue", "my.test.pack", "--no-dry-run", "--monochrome", "classpath:features" };
CucumberStaticRunner.startTests(cucumberOptions);

1
投票

通过以下配置修复:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
       <requiresUnpack>
           <dependency>
                 <groupId>info.cukes</groupId>
                 <artifactId>cucumber-java</artifactId>
          </dependency>
       </requiresUnpack>
    </configuration>
</plugin>

-1
投票

您应该添加cucumber-java依赖项

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>${cucumber.version}</version>
</dependency>

-1
投票
private static byte run(String[] argv, ClassLoader classLoader) throws IOException {

    // cucumber/Spring Boot classloader problem
    // CucumberException: No backends were found. Please make sure you have a backend module on your CLASSPATH

    RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));
    ResourceLoader resourceLoader = new MultiLoader(classLoader);
    ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
    Reflections reflections = new Reflections(classFinder);
    List<Backend> list = new ArrayList<>();
    list.addAll(reflections.instantiateSubclasses(Backend.class, "cucumber.runtime", new Class[]{ResourceLoader.class}, new Object[]{resourceLoader}));
    if (list.size() == 0) {
        JavaBackend javaBackend = new JavaBackend(resourceLoader);
        list.add(javaBackend);
    }
    Runtime runtime = new Runtime(resourceLoader, classLoader, list, runtimeOptions);
    runtime.run();
    return runtime.exitStatus();
}

-2
投票

我在POM.xml文件中使用了以下代码

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>

现在工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.