[hibernate-dev] Making tests nicer with lambdas

Sanne Grinovero sanne at hibernate.org
Sat Apr 26 18:42:46 EDT 2014


I love the idea, but I'm coming from a very different angle:
it's important that we regularly exercise our APIs, and verify that
they make sense in the new world.

But can we reliably run tests compiled with Java8 but still verify our
main library is going to run fine in previous JVMs?

I don't fully trust things like animal-sniffer, I want to actually run
our stuff on older VMs.

Sanne

On 25 April 2014 09:41, Gunnar Morling <gunnar at hibernate.org> wrote:
> Hey,
>
> I've played around a bit with the idea of using Java 8 lambdas to make
> tests easier to write and read. We have many tests which open a session and
> TX, do some stuff, commit, open a new TX (and/or session), do some
> assertions and so on:
>
>     Session session = openSession();
>   Transaction transaction = session.beginTransaction();
>
>     // heavy testing action...
>     transaction.commit();
>   session.clear();
>
>     transaction = session.beginTransaction();
>
>     // load, assert...
>   transaction.commit();
>   session.clear();
>
> The same could look like this using Java 8 lambdas:
>
>     Foo foo = inTransactionWithResult( (session, tx) -> {
>         // heavy testing action...
>   } );
>
>     inTransaction( (session, tx) -> {
>         // load, assert...
>   } );
>
> Extracting the session/TX handling removes quite some clutter and focuses
> more on the actual testing logic. It also avoids problems due to dangling
> transactions e.g. in case of assertion failures as the TX handling is done
> in a finally block in inTransaction().
>
> At this point I've just done a quick POC and would be interested in
> feedback whether you think that's worth pursuing or not. Note that
> different language levels can be used for test and main code, so we could
> make use of lambdas in tests while ensuring Java 6 compatibility for the
> delivered artifacts.
>
> --Gunnar
> _______________________________________________
> hibernate-dev mailing list
> hibernate-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/hibernate-dev


More information about the hibernate-dev mailing list