Re: JUnit-BDD

Bill Venners <[email protected]> Sat, 19 Apr 2014 16:12:24 -0700
Newsgroups gmane.comp.java.junit.user
Message-ID <CAO_Rykq66Z3_b4mGgVuhUK5FfcdHV2WBB2h0cKjP+wMhvMnoiQ@mail.gmail.com>
Hi Cedric,


On Sat, Apr 19, 2014 at 10:56 AM, Cédric Beust ♔ <[email protected]> wrote:

>
>
> On Fri, Apr 18, 2014 at 10:41 AM, David Saff <[email protected]> wrote:
>
>
>>
>> I think that Java 8 lambdas open up a lot of opportunities to accomplish
>> JUnit's goals in completely different ways.  I've been exploring with one
>> approach myself on and off.  For one thing, to date, JUnit has relied on
>> specially-named or annotated methods to accomplish the goal of succinctly
>> associating a name with a block of code.  In Java 8, one can just do:
>>
>> addTest("Some test name", () -> {
>>    // some code here
>> })
>>
> I don’t see a lot of added value for this over
>
> @Test(description = "Some test name")public void shouldDoSomething) { ... }
>
> Actually, you lose a lot of expressivity that annotations give you for
> free, such as adding configuration parameters to your test (e.g. specifying
> concurrency settings, inheriting annotations from a base class, etc…).
>
I believe you can do all that by passing in values to a method like addTest
as well, so I don't think you lose anything.


> I’ve tried to find ways to leverage lambdas as well for testing and I’ve
> come up completely empty so far.
>
Two ways to leverage lambdas are that because test lambdas would be
registered in order of appearance, the order of all the strings in the
output can match exactly the order of appearance of the source code. With
test methods (not test lambdas), because the Java reflection API does not
guarantee anything about the order of methods returned during discovery,
you can't be sure what the order was in the source. At least not by just
using reflection. What we do in ScalaTest for test methods is order them
all alphabetically so there's a defined order. Having the output match the
input is nice for specification-style tests.

Another is that you can dynamically register tests when the test bodies are
lambdas. This can be useful to build up tests whose bodies you want to run
on different fixture objects. In ScalaTest (and RSpec in Ruby) this is
called "shared tests". Here's an example:

http://doc.scalatest.org/2.1.3/index.html#org.scalatest.FunSuite@sharedTests

Another specific example is you may want to run the same test bodies using
different Selenium drivers, so you make sure your integration tests work in
multiple browsers.

Bill


> --
> Cedric
>
>  
>



-- 
Bill Venners
Artima, Inc.
http://www.artima.com