|
|
|
原文: [url=http://stackoverflow.com/questions/16036120/behavior-driven-development-for-java-what-framework-to-use]http://stackoverflow.com/questions/16036120/behavior-driven-development-for-java-what-framework-to-use
For the ongoing projects and for improving our development process we considered adopting TDD as development philosophy. While researching for best practices and how to "sell" the new approach to my colleagues/ developers I came across BDD and found it even more appropriate to what we would need and somehow to be next iteration of TDD. The problem is that up to now I tried only the [url=http://jbehave.org/]tool developed by Dan North, [url=http://jbehave.org/]JBehave and I cannot say that I am amazed.
The setup seems to me cumbersome and I couldn't find very appropriate documentation for it. On the other hand I tried also [url=https://code.google.com/p/spock/]spock the groovy tool and up to now I kind of like it.
Q: are there are proper tools to be used for BDD
Q: you would use instead spock and deal with the overhead of introducing another language
Thank you!
-----------
Behavior Driven Development is just a technique that can be used without any tools. You can just write tests in BDD style - e.g. start test methods with should and introduce some separate feature with this method. When and then sections can be replaced with just comments, e.g.
@Testpublic void should_do_something() { // given Something something = getSomething(); // when something.doSomething(); // then assertSomething(); // when something.doSomethingElse(); // then assertSomethingElse();}My opinion on the mentioned frameworks:
- The problem with [url=http://jbehave.org/]JBehave is that tests look like a complex spaceship. On the other hand it has pretty output for your specifications.
- [url=https://code.google.com/p/spock/]spock is really cool. Compact syntax, pretty output, a lot of features, written with the powerful groovy language, which means the possibility of usage in conjunction with [url=http://www.gebish.org/]geb. BUT it is groovy and it can be very important for someone.
- [url=http://www.scalatest.org/]scalatest (written with scala) and [url=http://easyb.org/]easyb (written with groovy) both have the same disadvantage as spock. The "... should ..." and "Given...Then" notation. Specifications are in .story files, and the step implementations are in Java classes. This approach work very well as a collaboration and communication tool to define the specs, but would usually be too much overhead for low-level coding.
- Cucumber is a widely-used tool from the Ruby world, which now has a JVM version that works in many languages. It uses a very similar "Given..When..Then" notation. Ports of Cucumber exist for .NET (SpecFlow), Python (Behave and Lettuce), PHP (Behat) and many more languages.
I also think that the most successful BDD frameworks for Java are those that are not written in Java, since the Java language has no such flexibility for DSL (Domain Specific Language) creation that Groovy or Scala has.
--------------
One persons opinion. I've had fantastic experiences with spock. No problem testing Java code, and groovy is incredibly easy for a Java programmer to pickup. You can start out writing regular Java and keep working, then gradually adopt the more terse style as you learn Groovy if desired. I was skeptical of Groovy, but spock is such a joy to use that it more than repays the minimal effort to learn a little bit of Groovy
-------------
Unless your product owner/qa/customer need to be able to read the tests, use [url=https://code.google.com/p/spock/]Spock. It is very simple tool, but improves readability of tests. Thanks to it's powerful features you don't need Mockito, Hamcrest nor AssertJ. And it has superb parametrized tests. In fact, it is "just" a better JUnit - a general tool for automated execution of simple tasks, be it unit tests, integration tests or acceptance tests.
Fearing Groovy? Why? It is very similar to java. The more you learn it, the more expressive and shorter your code is. Your tests will be shorter and more readable. Groovy is gateway drug to the better side of JVM.
Don't like dynamic languages? Well, it is tests, and tests are run by CI server after every commit, right? If your code breaks, you will know it after few minutes. Don't have CI server or not running tests regularly? Then don't bother with choosing a test framework and go fix your process. Broken tests are useless and if you don't run the tests regularly, they will break soon.
Go with JBehave/Cucumber if you need it; Otherwise, use Spock.
----------------
As the author of [url=http://jgiven.org/]JGiven I have to disagree with sody that Java has not enough flexibility for DSL creation. In JGiven, BDD tests looks as follows:
@Testpublic void users_can_login { given() .a_registered_user() .and().the_login_page_is_shown(); when() .the_user_enters_correct_credentials() .and().the_login_button_is_pressed(); then() .the_welcome_page_is_shown();}JGiven is used together with JUnit or TestNg and you write your tests in plain Java.
|
|