Sureassert Test Doubles are classes designed to replace a designated class within the testing context. That means that whenever Sureassert UC runs an Exemplar or JUnit, the test double class replaces the designated class.
To create a test double, create the test double class in the unit test source path of your project (typically src/java/test) and annotate it with the @TestDouble annotation. This annotation takes as a parameter the class of the “doubled class”, that is, the class that will be replaced by the Test Double class in the testing context.
Test double classes are useful for replacing (in the testing context) Delegate classes that provide an API into an external resource, for example classes that delegate calls to a service accessed over the network (web service, EJB, etc). Using delegate classes to access such services and providing a test double is a good technique for simplifying test isolation, especially where you want a stub class that performs some form of intelligent response.
The only restriction on the test double class is that it must contain the same publically-accessible method signatures as the doubled classes, for all those methods that are invoked by your project. If you project only invokes 1 method on the doubled class, then you only need to code that 1 method in the test double. The test double class does not need to extend or be part of the same hierarchy in any way, of the doubled class.
The doubled (production) class:
public class BillingServiceDelegate { private String[] someTestData; public Contract createNewContract(Customer customer, Handset handset, PhonePlan plan, double pricePM) { // Some production code to remotely connect to an external service } public void foo() { // Another service delegate method } private void bar() { // Some private code } }
The test double class:
@TestDouble(replaces=BillingServiceDelegate.class) public class BillingServiceDelegate_TD { public Contract createNewContract(Customer customer, Handset handset, PhonePlan plan, double pricePM) { // do not contact remote billing service; respond with // fixed or reactive test response. } // Note: do not need to replace any other methods of BillingServiceDelegate if not required. }
Sureassert test doubles work by replacing the doubled class in the test context with the test double class. Note that this means the test double class effectively is renamed to the doubled class, so you cannot reference the test double class itself in any test code as it won’t exist at runtime.