views:

98

answers:

5

I'm trying to use BDD in a very simple way, in order to minimize the amount of Java code. I want to create exactly two files, one is my story:

Given user is named "John Doe" 
And user is authenticated
When user changes his password to "a1b2c3"
Then user password equals to "a1b2c3"

Next, I create a Java class:

public class UserManipulator {
  @Given("$user is named $name")
  public User shouldExistOrBeCreated(String name) {
    User user = //...
    return user;
  }
  @Given("$user is authenticated")
  public void shouldBeLoggedIn() {
    // ...
  }
  @When("$user changes his password to $pwd")
  public void shouldChangePassword(User user, String pwd) {
    // ...
  }
  @Then("$user password equals to $pwd")
  public void shouldHaveThisPassword(User user, String pwd) {
    assertEquals(user.getPassword(), pwd);
  }
}

And that's it. I don't want to have any more files, any more unit tests. I want some BDD-framework to find my story file, parse all my Java files, and run them one by one. Is it possible to achieve?

ps. What is important here is a possible reuse of Java methods in my other stories. For example, this is the story no.2:

Given user is named "Michael Doe"   <-- reuse
When user adds $100.00 to his account
Then user account balance is $100.00
+3  A: 

You want to have a look at:

Also, this presentation on BDD in Java and Groovy could be of interest.

haylem
@haylem JBehave doesn't allow such an approach. I have to create an instance of JUnitStory class for every story. Which is an overhead, as I see it...
Vincenzo
@Vincenzo: Sorry, I didn't understand your requirement like that. I'm actually not sure I understand. You want a framework to extrapolate and run the test from this story and this Java class over all the Java files of your project? I don't see how that's useful.
haylem
@haylem The story itself has enough information about what I want to test and how (my scenario). I don't see why I would need to create any other classes on top of it. But in JBehave I have to create them and they are rather complex and error prone.. That's why I'm trying to find a framework which will do all this work for me, maybe on top of JBehave, why not...
Vincenzo
@Vincenzo: have a look at easyb. The syntax is even more lightweight, as it uses groovy.
haylem
@haylem I just reviewed their presentation, thanks. Easyb is just another mechanism of unit test writing. Yes, it is more lightweight than Java+JUnit, but it's still not what I'm looking for. I want to enable my stories to **reuse** Java constructs, catching them just by RegEx patterns.. I will update the question now to explain better.
Vincenzo
@Vincenzo: OK I understand your idea now. Unfortunately, I don't know of any tools to do that. It seems like a good idea but I don't think there's a widely accepted tool for this use case.
haylem
@haylem Maybe they just don't exist. Anyway, thanks for your response, I already up-voted it. If there won't be any more answers to my question, I will approve yours.
Vincenzo
@Vincenzo: Thanks for the upvote. Regarding accepting the answer, I honestly don't know what the right thing to do is in that case, as it's not exactly an answer to your question.
haylem
+1  A: 

Not really what you are looking for, but you may want to have a look at Spock.

Justin Muller
+1  A: 

I don't think it provides the level of reuse you're looking for but also have a look at Concordion, a BDD framework similar to Fitnesse but much easier to use (specifications are written in plain text, in the form of HTML pages). And it integrates just directly with JUnit and thus also with Maven.

See also

Pascal Thivent
+2  A: 

We use Cucumber, which is a Ruby framework but by bundling JRuby into your project you can easily access your Java objects. It does mean you write your step definitions in Ruby, but it also minimises the amount of Java you write :)

The story format in Cucumber is exactly as you describe in your example, and re-use of story lines is trivial.

Nick
@Nick You don't have any more JUnit tests, right? All you have is `.feature` files and JRuby code. Am I correct?
Vincenzo
For our specs, yeah. We fire up Cucumber directly from our Ant build, and don't need to create a Java class for each test or anything like that.
Nick
+1  A: 

The robotframework may be of interest. You can read the details in the user guide here: http://robotframework.googlecode.com/svn/tags/robotframework-2.5.4/doc/userguide/RobotFrameworkUserGuide.html#behavior-driven-style

Robotframework is written in python and new keywords can be implemented in python or jython.

There is also a thesis on the use of RF for ATDD here: http://www.niksula.cs.hut.fi/~jprantan/thesis/thesis_juha_rantanen.pdf

Mark Irvine