java - How to unit test to the case where there are two public methods, one calling another? -
what way test methods when there 2 public methods , method calls public method in same class?
how should write unit tests in scenario?
an example
class specificintmath { public int add(int a,int b) { return a+b; } public int multiply(int a, int b) { int mul = 0; for(int = 0;i<b,i++) { mul=add(a,mul); } return mul; } }
this example doesn't show complexity of both methods involved concept.
should test add
, multiply
separately? if should test multiply
only, feel miss out cases multiple
cannot provide parameters.
assuming multiply
, add
tested separately, should able mock add
? how possible?
assuming multiply
, add
tested separately , shouldn't mock, should let add
perform is. if case how should deal flow of program inside add
?
what approach test such kind of situation.
edit 1:
in below code,
class mcvc { public boolean getwherefrom(list<user> users) { boolean alldone = true; for(user user: users){ string url = user.geturl(); switch(url) { case consts.google: somedao.updatefromaddr(user); user.setentry("search engine"); break; case consts.facebook: somedao.updatefromaddr(user); user.setentry("social media"); break; case consts.home: somedao.updatetoaddr(user); user.setentry("company"); default user.setentry(null); alldone = false; break; } } return alldone; } public void likeddeck() { list<users> userslist = deckdao.getpotentialusers(345l,httpstatus.ok); boolean flag = getwherefrom(userslist); if(flag) { for(user user: userslist) { //some action } } } }
should consider getwherefrom()
while testing likeddeck()
or should assume default situation? if consider default situation, lose out on cases output isn't default. not sure should mock since class calling being tested. spying/mocking class under test
you don't care.
you use unit-testing test contract of each public method on own. write tests make sure both add()
, multiply()
supposed do.
the fact 1 uses other internally of no interest on outside. tests should neither know nor care internal implementation detail.
and record: code written right now; absolutely not turn mocking here. mocking not required here; , adds risk of testing has nothing real production code. only use mocking situations when have control aspects of objects in order enable testing. nothing in example code needs mocking tested. , if - indication of poor design/implementation (given contract of methods)!
edit; given changes example in question:
first of all, there bug in getwherefrom()
- iterate list; keep overwriting return value in list. when first iteration sets result false; information might lost in next loop.
i see 2 options actual question:
- you turn mockito; , "spy" concept partial mocking; in case want keep source code is
- me, personally; rather invest time improving production code. looks me
getwherefrom()
worth own class (where not have work on list of users; one user; helps returning single boolean value ;-). , when that, can use dependency injection acquire (mocked) instance of "wherefromservice" class.
in other words: code showing reworked/refactored; example more follow srp. of course larger undertaking; need discuss people around you.
Comments
Post a Comment