The online community for software testing & quality assurance professionals
 
 
Calendar   Today's Topics
Sponsors:




Lost Password?

Home
BetaSoft
Blogs
Jobs
Training
News
Links
Downloads



Software Testing >> Unit Testing

Pages: 1
Parthasarathi
Newbie


Reged: 11/08/11
Posts: 2
Stub by function in unit testing
      #691209 - 11/15/11 07:07 PM

Hello everyone,

Does stubbing the function is really required in Unit testing? If so what is the importance and purpose of stub the function during unit testing?

Can we do unit test without stubbing? What will happen if we do unit test without stubbing the function?

Thanks in advance


Post Extras: Print Post   Remind Me!   Notify Moderator  
dlai
Junior Member


Reged: 05/02/06
Posts: 1041
Loc: CA, USA
Re: Stub by function in unit testing [Re: Parthasarathi]
      #691290 - 11/16/11 08:38 AM

In unit testing, you are testing a class in complete isolation and control all the input/outputs going to that class, and everything that class interacts with is needed. For example, say you have a class 'Foo' that calls a method on the class 'Bar'

Code:

class Foo{
private Bar bar;
Foo(Bar b){
bar = b;
}

onIwasFooed() {
bar.onIwasBared();
}
}
Code:


Say you want to test that 'Bar'.onIwasBared() was called, when an event is sent to 'Foo'. You will need to create a stub or mock of 'Bar' to test 'Foo'. The easy way to do that is either write a stub (a dummy class implementing the same interface) or a mock (using the class itself to derive a mock object).

Code:

//stub
class BarStub implements IBarInterface {
boolean testPassed = false;
onIwasBared() {
testPassed = true;
}
}

//mock
Bar mockBar = (Bar)Mockito.mock(IBarInterface);
// do unit testing
Mockito.verify(mockBar).iwasBarred();



These days, I've been doing more mocking than stubbing. Mockito is pretty awesome and I've been able to get away without stubbing like 80% of the time.

--------------------
David Lai
Sr. QA / Test Lead
LinkedIn profile


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 3 anonymous users are browsing this forum.

Moderator:  AJ, Jeanj 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 2843

Rate this topic

Jump to

Contact Us | Privacy statement SQAForums

Powered by UBB.threads™ 6.5.5