testing - About use Stubs - Java -


i'm readin http://xunitpatterns.com/test%20stub.html , have questions use of stubs, example, in code shown on page author creates class called timeproviderteststub.java use in test code. have doubts line in test code:

timedisplay sut = new timedisplay();   //      test double installation   sut.settimeprovider(tpstub); 

do need modify class(sut) recieve 1 object timeprovidertestsub?

both stub , real class supposed implement interface, i.e. itimeprovider, , settimeprovider() should take interface parameter. interface must expose methods sut needs interact object, since timedisplay can use object through itimeprovider interface (which allows use stub instead of real object in our tests).

in example, sut (timedisplay) seems need gettime() method, interface should contain method:

public interface itimeprovider {     calendar gettime(); } 

the declaration of stub should be

public class timeproviderteststub implements itimeprovider { ... } 

and declaration of real class should be

public class timeprovider implements itimeprovider { ... } 

finally, sut must change setter method accept interface:

public void settimeprovider(itimeprovider timeprovider) { ... } 

and change internal timeprovider field of type itimeprovider.

if not control code of real class (so cannot make implement interface), can create adapter class wraps real class , implements interface.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -