2012-10-18 65 views
2

我對jbehave甚至是自動化測試完全陌生。 我在線閱讀教程並嘗試遵循以下步驟。在eclipse中使用jbehave執行項目的問題

我想在eclipse IDE中運行這個應用程序。

我做了其中包含的測試相關的Math.story文件:

Scenario: 2 squared 
Given a variable x with value 2 
When I multiply x by 2 
Then x should equal 4 

在叫ExampleSteps.java一個.java文件,這些步驟都寫:

import org.jbehave.core.annotations.Given; 
import org.jbehave.core.annotations.Named; 
import org.jbehave.core.annotations.Then; 
import org.jbehave.core.annotations.When; 
import org.jbehave.core.steps.Steps; 

public class ExampleSteps extends Steps { 
    int x; 

    @Given("a variable x with value $value") 
    public void givenXValue(@Named("value") int value) { 
     x = value; 
    } 

    @When("I multiply x by $value") 
    public void whenImultiplyXBy(@Named("value") int value) { 
     x = x * value; 
    } 

    @Then("x should equal $value") 
    public void thenXshouldBe(@Named("value") int value) { 
     if (value != x) 
      throw new RuntimeException("x is " + x + ", but should be " + value); 
    } 
} 

我創建了另一個類SimpleJbehave其中主要方法有: import java.util.Arrays; import java.util.List;

import org.jbehave.core.embedder.Embedder; 

public class SimpleJBehave { 

    private static Embedder embedder = new Embedder(); 
    private static List<String> storyPaths = Arrays 
      .asList("Math.story"); 

    public static void main(String[] args) { 
     embedder.candidateSteps().add(new ExampleSteps()); 
     embedder.runStoriesAsPaths(storyPaths); 

    } 
} 

當我運行這段代碼,我得到以下異常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer 
    at org.jbehave.core.configuration.Configuration.<init>(Configuration.java:112) 
    at org.jbehave.core.configuration.MostUsefulConfiguration.<init>(MostUsefulConfiguration.java:49) 
    at org.jbehave.core.embedder.Embedder.<init>(Embedder.java:30) 
    at org.jbehave.core.embedder.Embedder.<init>(Embedder.java:37) 
    at SimpleJBehave.<clinit>(SimpleJBehave.java:8) 
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 5 more 

由於我是新手,我一直無法理解究竟是什麼問題。

如果有人能告訴我該怎麼做才能讓這段代碼正常工作,那將會非常好。 我的方法錯了嗎?

非常感謝您提前。

回答

1

看起來您的類路徑上沒有org.apache.commons.collections.Transformer。看起來這個類可以在這裏的apache-commons-transformer庫中獲得:http://commons.apache.org/collections/api-release/org/apache/commons/collections/Transformer.html

下載jar並將它添加到你的類路徑中。它可能工作。

+0

我添加了apache-collections-commons-collections-3.1.jar,freemarker-2.3.6.jar,jbehave-core-3.0.2.jar,org.apache.commons.io.jar和org.apache.commons .lang.jar到路徑。 但現在,我無法在outputDirectory中生成故事視圖。 它是由org.jbehave.core.reporters.FreemarkerViewGenerator $ ViewGenerationFailedForTemplate引起的。 我不知道該如何處理?我創建的.story文件有問題嗎?因爲它說,「沒有一步是匹配的」。 我真的很陌生,根本不知道我應該做什麼。 謝謝。 – Sathya

+0

@Sathya:你找到了解決這個問題的任何解決方案,我面臨同樣的問題,我剛開始研究Jbehave,請讓我知道是否有解決這個問題。 在此先感謝! – Srivastava