无法使用Maven运行JBehave

时间:2014-08-05 09:46:14

标签: maven jbehave jbehave-plugin jbehave-maven-plugin

我跟随了一个带有maven的JBehave教程,但不幸的是它似乎过时了。所以我尝试将它与新教程(来自官方网站)结合起来。经过一些修改后,故事就像以前一样无法运行。

但是这是我的项目文件夹树

├── jbehave-demo.iml
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   │       └── stories
│   │           └── de
│   │               └── codecentric
│   │                   └── jbehave
│   │                       └── stack_scenarios_test.story
│   └── test
│       └── java
│           └── de
│               └── codecentric
│                   └── jbehave
│                       ├── StackScenariosTest.java
│                       └── StackSteps.java

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/maven-v4_0_0.xsd">

    <url>http://maven.apache.org</url>
    <modelVersion>4.0.0</modelVersion>
    <name>JBehaveDemo</name>
    <groupId>de.codecentric</groupId>
    <artifactId>jbehave-demo</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.jbehave</groupId>
            <artifactId>jbehave-core</artifactId>
            <version>3.9.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jbehave</groupId>
                <artifactId>jbehave-maven-plugin</artifactId>
                <version>3.9.3</version>
                <executions>
                    <execution>
                        <id>run-stories-as-embeddables</id>
                        <phase>integration-test</phase>
                        <configuration>
                            <includes>
                                <include>**/*Test.java</include>
                            </includes>
                            <ignoreFailureInStories>true</ignoreFailureInStories>
                            <ignoreFailureInView>false</ignoreFailureInView>
                            <systemProperties>
                                <property>
                                    <name>java.awt.headless</name>
                                    <value>true</value>
                                </property>
                            </systemProperties>
                        </configuration>
                        <goals>
                            <goal>run-stories-as-embeddables</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

最后一个,这是我的Test类(运行Jbehave)

public abstract class StackScenariosTest extends JUnitStory {

    private final CrossReference xref = new CrossReference();

    public StackScenariosTest() {
        configuredEmbedder().embedderControls().doGenerateViewAfterStories(true).doIgnoreFailureInStories(true)
                .doIgnoreFailureInView(true).useThreads(2).useStoryTimeoutInSecs(60);
    }

    @Override
    public Configuration configuration() {
        Class<? extends Embeddable> embeddableClass = this.getClass();
        Properties viewResources = new Properties();
        viewResources.put("decorateNonHtml", "true");
        ParameterConverters parameterConverters = new ParameterConverters();
        ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(new LocalizedKeywords(),
                new LoadFromClasspath(embeddableClass), parameterConverters);
        // add custom converters
        parameterConverters.addConverters(new ParameterConverters.DateConverter(new SimpleDateFormat("yyyy-MM-dd")),
                new ParameterConverters.ExamplesTableConverter(examplesTableFactory));


        URL storyURL = null;
        try {
            // This requires you to start Maven from the project directory
            storyURL = new URL("file://" + System.getProperty("user.dir")
                    + "/src/main/resources/stories/");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        return new MostUsefulConfiguration()
                .useStoryControls(new StoryControls().doDryRun(false).doSkipScenariosAfterFailure(false))
                .useStoryLoader(new LoadFromRelativeFile(storyURL))
                .useStoryParser(new RegexStoryParser(examplesTableFactory))
                .useStoryPathResolver(new UnderscoredCamelCaseResolver())
                .useStoryReporterBuilder(
                        new StoryReporterBuilder()
                                .withCodeLocation(CodeLocations.codeLocationFromClass(embeddableClass))
                                .withDefaultFormats().withPathResolver(new FilePrintStreamFactory.ResolveToPackagedName())
                                .withViewResources(viewResources).withFormats(Format.CONSOLE, Format.TXT, Format.HTML, Format.XML)
                                .withFailureTrace(true).withFailureTraceCompression(true).withCrossReference(xref)
                )
                .useParameterConverters(parameterConverters)
                        // use '%' instead of '$' to identify parameters
                .useStepPatternParser(new RegexPrefixCapturingPatternParser("%"))
                .useStepMonitor(xref.getStepMonitor());
    }


    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration(), new StackSteps());
    }

}

当我想从终端运行JBehave时,我执行了这个命令:

mvn integration-test

然而,这是结果(我没有把所有的结果都放在一起)

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jbehave-demo ---
[INFO] Surefire report directory: /Users/dev/Desktop/cc_JBehaveSample/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jbehave-demo ---
[INFO] Building jar: /Users/dev/Desktop/cc_JBehaveSample/target/jbehave-demo-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- jbehave-maven-plugin:3.9.3:run-stories-as-embeddables (run-stories-as-embeddables) @ jbehave-demo ---
[INFO] Running stories as embeddables using embedder Embedder[storyMapper=StoryMapper,storyRunner=StoryRunner,embedderMonitor=MavenEmbedderMonitor,classLoader=EmbedderClassLoader[urls=[/Users/dev/Desktop/cc_JBehaveSample/target/classes/, jbehave-core-3.9.3.jar, hamcrest-core-1.3.jar, hamcrest-library-1.3.jar, hamcrest-integration-1.3.jar, commons-collections-3.2.1.jar, commons-io-2.4.jar, commons-lang-2.6.jar, plexus-utils-3.0.10.jar, freemarker-2.3.19.jar, paranamer-2.4.jar, xstream-1.4.5.jar, xmlpull-1.1.3.1.jar, xpp3_min-1.1.4c.jar, junit-4.4.jar],parent=ClassRealm[plugin>org.jbehave:jbehave-maven-plugin:3.9.3, parent: sun.misc.Launcher$AppClassLoader@2503dbd3]],embedderControls=UnmodifiableEmbedderControls[EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=true,ignoreFailureInView=false,verboseFailures=false,verboseFiltering=false,storyTimeoutInSecs=300,failOnStoryTimeout=false,threads=1]],embedderFailureStrategy=<null>,configuration=org.jbehave.core.configuration.MostUsefulConfiguration@37b57b54,candidateSteps=<null>,stepsFactory=<null>,metaFilters=<null>,systemProperties={java.awt.headless=true},executorService=<null>,executorServiceCreated=false,storyManager=<null>]

你有什么想法,为什么没有执行测试?我在控制台上找不到故事执行报告。谢谢。

1 个答案:

答案 0 :(得分:3)

您缺少的是您的配置中的测试。

             <executions>
                <execution>
                    <id>run-stories-as-embeddables</id>
                    <phase>integration-test</phase>
                    <configuration>
                        **<scope>test</scope>**
                        <includes>
                            <include>**/*Test.java</include>
                        </includes>
                        <ignoreFailureInStories>true</ignoreFailureInStories>
                        <ignoreFailureInView>false</ignoreFailureInView>
                        <systemProperties>
                            <property>
                                <name>java.awt.headless</name>
                                <value>true</value>
                            </property>
                        </systemProperties>
                    </configuration>
                    <goals>
                        <goal>run-stories-as-embeddables</goal>
                    </goals>
                </execution>