java - How to verify if an argument passed is with correct parameters if that argument's state is changed? -
i'm trying verify object using
@captor argumentcaptor<user> userargumentcaptor ...//define user name , jailname - var = user1 ...//call method under test. verify(userdao).getname(userargumentcaptor.capture()); verify(userdai).getjailname(userargumentcaptor.capture()); assertthat(userargumentcaptor.getallvalues()).isequalto(arrays.aslist(user1user1)); /** * notice here had use same object verify when captured in 2 different situations. */
for scenario,
user user = new user(); //object under inspection. user.setid(); //changing state here. userdao.getname(user); //capture user here. ... if(somebooleancondition) { user.setjailstatus(true); //changing state here. userdao.getjailname(user); //capture user here. }
while asserting userargumentcaptor.getvalue()
, checking updated value. makes sense since i'm capturing object not object's state.
but, how verify object's state when passed?
i think not possible. when checking out latest version of argumentcaptor (mockito 2.7.21 @ point); there no indication @ go in direction.
the purpose respectively service offered argumentcaptor collect parameters used when method called.
what asking basically: there way additional checking @ point when call happens.
i guess, made possible: framework have allow register callback, called whenever specific method invoked on mock object. isn't possible today.
the solution see - instead of doing
@mock yourrealdaoclass themockeddao
you have do
class mockeddaoclass extends yourrealdaoclass {
and implement such things yourself. can put:
yourrealdaoclass themockeddao = new mockeddaoclass(...
into tests. other logic enable things need.
in other words: mocks generated mockito not allow enhanced testing @ point when methods invoked on mocked objects. if need (for example test like: param.getfoo().equals()
) creating own stub/mock class.
what have do:
- carefully check methods dao class has
- decide 1 need overwrite, make sure return values need make mock work production code
- for methods receive parameters interested in: either checking right there; or collect values later checking
Comments
Post a Comment