MByHave is a BDD (Behaviour Driven Development) tool for Java. Very similar to JBehave but it is more simple and easy to integrate to JUnit.
MByHave is delivered with a JUnit runner (plugin) which can parse story files and match them to the annotated test file methods.
Pros and cons
Pros
- Very simple
- Easy-to-use
- Easy to configure
- JUnit plugin
Cons
- No reporting subsystem
- Just a JUnit plugin
Download
- 1.0 Alpha - https://github.com/downloads/bsudy/mBy.have/mBy.have-1.0.0-ALPHA.jar
- Source - https://github.com/bsudy/mBy.have
How to use
Step 1. - Annotate your methods.
You can use the following annotations on your methods: @Given, @When and @Then. Each annotation takes a parameter, called value, which is a pattern which to the step descriptions will be matched later. The pattern can contain any text and placeholders of the method parameters. The parameters during the matching will be picked up from the step description and past to the method as defined parameter.
Comment: Currently there is no autoconvert, the parameters of the methods have to be Strings.
Examples:
Given without parameter:
@Given("something")
public void givenMethodName() {
<<test logic>>
}
Given with parameters:
@Given("something with two parameters: $param1 and $param2)
public void givenWithParameter(final String param1, final String param2) {
<<test logic>>
}
When with parameters:
@When("anything is $param")
public void whenMethod(final String param) {
<<test logic>>
}
Then with parameters:
@Then("the result is $param")
public void thenMethod(final String param) {
<<test logic>>
}
The methods can't send information to each other through the MByHave API, the state of the test has to stored in the test class.
Step 2. - Write tests
There are 3 ways to write tests but the third way is the officially suggested way.
1 way: Step parser
The annotated step methods can be invoked from code as individual steps. The MByHave contains three methods to invoke the annotated methods with step description text.
The MByHave#given(String) method tries to match a @Given annotated method to the parameter string. The MByHave#when(String) does the same with @When methods, the mByHave#then(String) with the @Then methods.
Example:
new MByHave(this).given("something").
when("anything is first test").
then("the result is success");
This test will invoke first the givenMethodName() method then the whenMethod() with "first test" parameter and last the thenMethod() with "success" parameter.
Comment: There is no restriction on the order and the number of the step descriptions. For example you can create a test like:
@Test
public void testFirstWay() {
new MByHave(this).given(...).
given(...).
then(...).
given(...).
when(...).
then(...).
given(...);
}
2. way: Scenario parser
A series of steps is called as Scenario. A scenario also can be specified and run as one string. The MByHave has a runScenario method that parses and matches the steps to the annotated step methods.
Example:
@Test
public void testSecondWay() {
new mByHave(this).runScenario("Scenario this is a scenario description" +
"Given something" +
"When anything is second test" +
"Then the result is success")
}
As in the first way the test will invoke the givenMethodName(), the whenMethod() with "second test" and the thenMethod() with "success" parameters.
3. way: Story file parser
This is the most common way. The Scenarios can be organized in Story files. Each story file can contain one or more scenarios. The MByHave parses the stories and runs them. Each scenario runs on a newly created object, so the test class has to contain a default constructor. (or has to contain constructor not at all).
To run these tests you have to apply the MByHaveRunner JUnit runner and a Story annotation on the step definition class. The story description file has to defined as a parameter of the Story annotation and has to be in the package root or the same package as the step definition class is.
Example:
Test class:
@RunWith(mByHaveRunner.class)
@Story("storyFile.story")
public class TestSteps {
@Given("something")
public void givenMethodName() {
<<test logic>>
}
@Given("something with two parameters: $param1 and $param2)
public void givenWithParameter(final String param1, final String param2) {
<<test logic>>
}
@When("anything is $param")
public void whenMethod(final String param) {
<<test logic>>
}
@Then("the result is $param")
public void thenMethod(final String param) {
<<test logic>>
}
}
storyFile.story
Scenario definition #Comment Scenario this is a first scenario description Given something When anything is first test The result is success Scenario definition #Comment Scenario this is a second scenario description Given something with two parameters first test parameter and second test parameter When anything is second test The result is success
Running the test the following methods will be invoked:
- First scenario
- StepTest instantiation
- givenMethodName()
- whenMethod("first test")
- thenMethod("success")
- Second scenario
- StepTest instantiation
- givenWithParameter("first test parameter", "second test parameter")
- whenMethod("first test")
- thenMethod("success")
Roadmap
v1.0
@Before, @After, @BeforeClass and @AfterClass support- more javadoc
- code cleanup - split up MByHaveRunner.
v1.1
- auto parameter type conversion
- NetBeans test result fix
v1.2
- And keyword
- @Rule Junit annotation support.
v2.0
- android integration
Download
- 1.0 Alpha - https://github.com/downloads/bsudy/mBy.have/mBy.have-1.0.0-ALPHA.jar
- Source - https://github.com/bsudy/mBy.have
License
The code is under BSD License but JUnit with the Common Public Licence and Paranamer with Creative Common License are linked.
No comments:
Post a Comment