Re: junit-nested: TestRunner for non-static inner classes

Esko Luontola <[email protected]> Fri, 16 Aug 2013 12:35:28 +0300
Newsgroups gmane.comp.java.junit.user
Message-ID <[email protected]>
Aaron VonderHaar wrote on 16.8.2013 5:05:
> Cool, I did take a look at specsy before I started Nested, but I didn't
> want to use anonymous classes for each test, and I'm not quite ready to
> commit to JDK 8 or scala/groovy yet.

Yeah, that's understandable. JDK 8 is still at beta and changing the 
language can be a big change.

In some projects I'm using https://github.com/orfjackal/retrolambda to 
get lambda expressions on Java 7. I compile the source with JDK 8 and 
then use Retrolambda to convert the lambdas into regular anonymous 
clases, so that they will run on earlier JREs.

> As for test isolation, currently each each inner class instance gets its
> own independent outer class instance, so everything should be completely
> isolated.

Good. That's the way I like it and how JUnit does it. TestNG users may 
disagree. ;) For some reason not all understand it: 
http://stackoverflow.com/questions/1052577/why-must-junits-fixturesetup-be-static

I have one idea that how to implement a shared instance for all test 
methods, without violating the default test isolation. The test class 
could take as constructor parameter an object, which the test runner 
will instantiate only once and pass the same instance to all test class 
instances. Then that injected object could be used to hold shared state 
(e.g. database connection) without having to use static fields.

I'm planning to implement that in Specsy. Already now it should be 
possible to implement it at the level of one test class, but in the 
future I'll improve Jumi to support implementing that at the level of 
_all_ test classes in the suite. I'll make it so that only one Driver 
[1] instance (it corresponds JUnit's Runner class) is created and it 
will be used for all test classes. Also the Driver will receive an event 
when the suite is finished (for teardown of the shared state).

[1] http://jumi.fi/api/jumi-api/fi/jumi/api/drivers/Driver.html

-- 
Esko Luontola
www.orfjackal.net