我正在使用JBehave和Maven来测试Spring MVC应用程序。我正在使用单个故事文件运行下面的代码。我的问题是我经常遇到StoryExecutionFailed
错误,我不知道为什么或如何解决它。
我正在配置JBehave的方式有些可疑,因为我没有看到我的一些选项实际上被打印到输出(例如:输出显示默认超时为300秒,但我已明确将其设置为10000 in码)。然而我的故事课确实在运行,因为如果我添加System.out.println()
,我会看到输出打印到屏幕上。我也怀疑故事的步骤没有被映射,因为通常我会在输出中看到步骤代码。
我希望有人可以帮助排除故障,因为我不知道从哪里开始调试。
到目前为止我检查了什么:
storyPaths()
方法中打印出来,打印在此帖子中省略)编辑:我已经尝试将Eclipse附加到远程调试会话as outlined here,但即使maven等待它连接我的断点也不会停止执行。
我开始做这样的测试:
mvn clean package
我看到的错误是:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.myproject.test.behavior.stories.APIStories
Processing system properties {}
Using controls EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=false,ignoreFailureInView=false,verboseFailures=false,verboseFiltering=false,storyTimeouts=300,threads=1,failOnStoryTimeout=false]
Running story stories/PROJ/PROJ-689.story
Using timeout for story PROJ-689.story of 300 secs.
Generating reports view to 'C:\Users\acymmer\Projects\myproject\target\jbehave' using formats '[ide_console, html, stats]' and view properties '{navigator=ftl/jbehave-navigator.ftl, views=ftl/jbehave-views.ftl, reports=ftl/jbehave-reports.ftl, nonDecorated=ftl/jbehave-report-non-decorated.ftl, decorated=ftl/jbehave-report-decorated.ftl, maps=ftl/jbehave-maps.ftl}'
Reports view generated with 0 stories (of which 0 pending) containing 0 scenarios (of which 0 pending)
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.584 sec <<< FAILURE! - in com.mycompany.myproject.test.behavior.stories.APIStories
run(com.mycompany.myproject.test.behavior.stories.APIStories) Time elapsed: 1.574 sec <<< ERROR!
org.jbehave.core.embedder.Embedder$RunningStoriesFailed: Failures in running stories:
stories/PROJ/PROJ-689.story: org.jbehave.core.embedder.StoryManager$StoryExecutionFailed: stories/PROJ/PROJ-689.story
at org.jbehave.core.embedder.Embedder$ThrowingRunningStoriesFailed.handleFailures(Embedder.java:553)
at org.jbehave.core.embedder.Embedder.handleFailures(Embedder.java:238)
at org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:216)
at org.jbehave.core.junit.JUnitStories.run(JUnitStories.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Results :
Tests in error:
APIStories>JUnitStories.run:20 ▒ RunningStoriesFailed Failures in running stor...
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.919 s
[INFO] Finished at: 2017-04-04T16:47:58-04:00
[INFO] Final Memory: 41M/374M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project MyProject: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\acymmer\Projects\myproject\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
场景文件文字:
Scenario: Run a JBehave test successfully
Given this jbehave scenario
When mvn integration-test is run
Then expect success
故事类:
package com.mycompany.myproject.test.behavior.stories;
import static java.util.Arrays.asList;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.embedder.EmbedderControls;
import org.jbehave.core.failures.PassingUponPendingStep;
import org.jbehave.core.failures.PendingStepStrategy;
import org.jbehave.core.i18n.LocalizedKeywords;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.io.StoryLoader;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.model.ExamplesTableFactory;
import org.jbehave.core.parsers.RegexStoryParser;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StepdocReporter;
import org.jbehave.core.reporters.StoryReporter;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.ParameterControls;
import org.jbehave.core.steps.ParameterConverters;
import org.jbehave.core.steps.ParameterConverters.DateConverter;
import org.jbehave.core.steps.ParameterConverters.ExamplesTableConverter;
import org.jbehave.core.steps.spring.SpringApplicationContextFactory;
import org.jbehave.core.steps.spring.SpringStepsFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import com.mycompany.myproject.test.behavior.infrastructure.JiraCredentials;
import com.jbehaveforjira.javaclient.JiraStepDocReporter;
import com.jbehaveforjira.javaclient.JiraStoryReporter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Configuration for running JBehave stories.
* This class contains configuration for running stories and reporting results to Jira.
*/
@ContextConfiguration("classpath*:jbehave-app-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class APIStories extends JUnitStories {
private JiraCredentials jiraCredentials = new JiraCredentials();
public APIStories() { }
@Override
public Configuration configuration() {
ParameterConverters parameterConverters = new ParameterConverters();
ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(
new LocalizedKeywords(),
new LoadFromClasspath(this.getClass()),
parameterConverters);
parameterConverters.addConverters(
new DateConverter(new SimpleDateFormat("yyyy-MM-dd")),
new ExamplesTableConverter(examplesTableFactory));
return new MostUsefulConfiguration()
.useStoryReporterBuilder(CreateStoryReportBuilder())
.useStoryParser(new RegexStoryParser(examplesTableFactory))
.useStoryLoader(CreateStoryLoader())
.useStepdocReporter(CreateStepdocReporter())
.useParameterControls(CreateParameterControls())
.useParameterConverters(parameterConverters)
.usePendingStepStrategy(new PassingUponPendingStep());
}
@Override
public InjectableStepsFactory stepsFactory() {
ApplicationContext appContext = new SpringApplicationContextFactory("jbehave-app-context.xml")
.createApplicationContext();
return new SpringStepsFactory(configuration(), appContext);
}
@Override
protected List<String> storyPaths() {
return new StoryFinder().findPaths(
CodeLocations.codeLocationFromClass(this.getClass()).getFile(),
asList("**/" + System.getProperty("storyFilter", "*") + ".story"),
null); //no excludes
}
private StoryReporterBuilder CreateStoryReportBuilder() {
StoryReporterBuilder b = new StoryReporterBuilder() {
public StoryReporter reporterFor(String storyPath, org.jbehave.core.reporters.Format format) {
if (format.equals(org.jbehave.core.reporters.Format.HTML)) {
return new JiraStoryReporter(
new File("target", "story_report.xml"),
keywords(),
jiraCredentials.GetUrl(),
jiraCredentials.GetProject(),
jiraCredentials.GetUserName(),
jiraCredentials.GetPassword(),
jiraCredentials.GetEnvironment());
} else {
return super.reporterFor(storyPath, format);
}
}
};
b = b.withCodeLocation(CodeLocations.codeLocationFromClass(this.getClass()))
.withFailureTrace(true)
.withFormats(Format.IDE_CONSOLE, Format.HTML, Format.STATS);
return b;
}
public StoryLoader CreateStoryLoader() {
return new LoadFromClasspath(APIStories.class);
}
public StepdocReporter CreateStepdocReporter() {
return new JiraStepDocReporter(
jiraCredentials.GetUrl(),
jiraCredentials.GetProject(),
jiraCredentials.GetUserName(),
jiraCredentials.GetPassword());
}
public EmbedderControls CreateEmbedderControls() {
return new EmbedderControls()
.doSkip(true) //allows use of the @skip meta annotation on stories?
.doIgnoreFailureInStories(false)
.doIgnoreFailureInView(true)
.doGenerateViewAfterStories(true)
.doVerboseFailures(true)
.doVerboseFiltering(true)
.useStoryTimeouts("10000"); //temporarily ensure timeouts are not an issue
}
public ParameterControls CreateParameterControls() {
return new ParameterControls().useDelimiterNamedParameters(true);
}
}
来自我的POM的相关位(注意我们在测试范围中使用它,与实际应用程序在同一模块中):
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<scope>test</scope>
<testSourceDirectory>${basedir}/src/test/java/</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/test-classes/</testClassesDirectory>
<includes>
<include>com/mycompany/myproject/test/behavior/stories/*.java</include>
</includes>
<printSummary>true</printSummary>
</configuration>
</plugin>
...
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>4.0.4</version>
<configuration>
<scope>test</scope>
</configuration>
<executions>
<execution>
<id>run-stories</id>
<phase>integration-test</phase>
<configuration>
<scope>test</scope>
<includes>
<include>com/mycompany/myproject/test/behavior/stories/*.java</include>
</includes>
<systemProperties>
<property>
<name>java.awt.headless</name>
<value>true</value>
</property>
</systemProperties>
<threads>1</threads>
<skip>false</skip>
<metaFilters>
<metaFilter>-skip</metaFilter>
</metaFilters>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
<execution>
<id>report-stepdocs</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>com/mycompany/myproject/test/behavior/stories/*.java</include>
</includes>
</configuration>
<goals>
<goal>report-stepdocs-as-embeddables</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
我的Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- =============================================================================================================== -->
<context:annotation-config />
<context:component-scan base-package="com.mycompany.myproject.test.behavior" />
</beans>
最后我的步骤类:
@Component
public class EmptyJBehaveSteps {
@BeforeStory
public void BeforeStoryStarts() { }
@Given("this jbehave scenario")
public void Given() { }
@When("mvn integration-test is run")
public void When() { }
@Then("expect success")
public void Then() { }
@AfterStory
public void AfterStoryEnds() { }
}
答案 0 :(得分:0)
使用maven-surefire-plugin version
2.19.1
,将此代码<version>2.19.1</version>
添加到maven-surefire-plugin
。如果你想要安装lib,你可以运行mvn clean install -DskipTests。并右键单击项目 - &gt; &#34;作为Maven测试运行&#34;。这将自动下载丢失的插件。 &安培;之后,右键单击项目 - &gt;&#34;更新Maven项目&#34;它删除了错误