White Box Testing
Test your code
   Home      TestPattern-SelfShunt
 

Self-Shunt Pattern
 
                  Test Driven Development by Kent Beck
 
Self shunt used in case of object under test communicates to another object and test case work as intermediary. With self shunt pattern object under test communicates to test case rather than  communicating another object.
 
Self shunt  uses the test-case object itself as a stub.
 
The term self-shunting is taken from the medical practice, a shunt is a hole or a small passage which moves, or allows movement of fluid from one part of the body to another.
 
 
For example take scenario of MailServer and MailClient communicate each other to send mail
The same can be implemented with help of self shunt
 
// To communicate object under test MailServer, MailServerTest  implements MailClient
public class MailServerTest extends TestCase implements MailClient
{
    public MailServerTest (String name) {
        super (name);
    }

    //Test method to test send mail
    public void testSendMail () {
        // MailServer accepts MailClient object, pass self reference as MailServerTest  implements MailClient
        MailServer mailserver = new MailServer (this);

        // sendmail calls internally mailRecieved in MailClient
        mailserver.sendmail ();
        assertEquals (new Email (“admin@whiteboxtest.com”,”hi”,”This is test mail”),this.mailrecieved);
    }

    void mailRecieved(Email mail) {
        mailrecieved = mail;
    }

    private Email mailrecieved;
}