2017-04-04 35 views
0

我使用JBehave和Maven來測試Spring MVC應用程序。我使用單個故事文件運行下面的代碼。我的問題是,我經常得到一個StoryExecutionFailed錯誤,我不知道爲什麼或如何解決它。JBehave Maven RunningStories失敗的異常

有一些可疑的配置JBehave的方式,因爲我沒有看到我的一些選項實際上正在打印輸出(例如:輸出顯示默認超時時間爲300秒,但我已明確將其設置爲10000在代碼中)。然而,我的故事課程確實在運行,因爲如果我添加System.out.println()的話,我會看到輸出結果被打印到屏幕上。我也懷疑故事的步驟沒有被映射,因爲通常我會看到輸出中打印的步驟代碼。

我希望有人可以幫助排除故障,因爲我不知道從哪裏開始調試。

我迄今檢查:

  • 故事文本註釋匹配我的步級
  • 在Spring上下文
  • 類限定符實際的類限定符
  • Spring註解是在正確的地方匹配(@Component)
  • 故事路徑不爲空或爲空(我在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

我沒有找到滿意的答案,我不再爲這個客戶工作,所以我不能回到代碼。但是,我確實將問題的至少一部分縮小到了JBehave公佈的「out-in」配置優先級(請參閱http://jbehave.org/reference/latest/configuration.html上的最後一個綠色框)。另一個懸而未決的問題來自JiraReporter,這是第三方軟件包的一部分,它使用jbehave輸出更新jira故事,這似乎需要從jira下載故事,而不是使用本地配置中的路徑。 –

回答

0

使用maven-surefire-plugin version2.19.1,添加以下代碼<version>2.19.1</version>maven-surefire-plugin。如果你想安裝lib,你可以運行mvn clean install -DskipTests。並右鍵單擊項目 - >「作爲Maven測試運行」。這會自動下載缺少的插件。 &之後,右鍵單擊項目 - >「更新Maven項目」,它將刪除錯誤