SUT - the type of the subject-under-test@FunctionalInterface public interface J8UnitTest<SUT>
Base interface of subject-under-test (SUT) based J8Unit tests. It straightforwardly specifies a SUT factory method to query fresh SUT instances.
There is no technical requirement to use this interface, but its usage is strongly recommend. Using this interface allows seamless combination of different J8Unit tests interfaces (even if specified by programmers of different APIs).
The implementation of createNewSUT() can be stand-alone, but it perfectly fits to Before
scenarios. Both scenarios are shown below (whereas FoobarTests extends J8UnitTest<Foobar>); Just adopt the
examples to your custom needs:
import org.j8unit.runners.J8Unit4;
import org.j8unit.usage.Foobar;
import org.junit.runner.RunWith;
@RunWith(J8Unit4.class)
public class FoobarTestCase
implements FoobarTests {
@Override
public Foobar createNewSUT() {
return new Foobar();
}
}
import org.j8unit.runners.J8Unit4;
import org.j8unit.usage.Foobar;
import org.junit.runner.RunWith;
@RunWith(J8Unit4.class)
public class FoobarTestCase
implements FoobarTests {
private Foobar foobar;
@Before
public void clear() {
this.foobar = new Foobar();
}
@Override
public Foobar createNewSUT() {
return this.foobar;
}
}
In case the SUT is not created directly but derived from an according SUT factory, FactoryBasedJ8UnitTest
should be used in preference. (Most often, this is the case when running parameterised tests.)
FactoryBasedJ8UnitTest| Modifier and Type | Method and Description |
|---|---|
SUT |
createNewSUT()
Factory method to create a new subject-under-test (of type
SUT). |
SUT createNewSUT()
Factory method to create a new subject-under-test (of type SUT).
If the specific SUT is deeply immutable (and, thus, cannot be altered in any circumstance), it is absolutely allowed to return the same SUT instance each time this method is called. If, otherwise, the SUT is mutable, a fresh instance must be returned to prevent any strange side-effect when asserting specific behaviour during the unit tests.
To say it clear: You do not know what specific usage will be executed on the SUT instance afterwards! So, think twice before carelessly returning a single instance again and again! Keep in mind internal caches, states, buffers, and further prevent SUT instances from being immutable; Immutability cannot be detected by looking at the visible elements only.
Copyright © 2015. All rights reserved.