java - Mockito: When is @Mock object get initialized and which constructor it calls -
i'm trying figure out how mockito working behind in order debug. wondering object @mock annotation, when initialized? like, before @before or after @before?
and if there're several different constructors, how mockito determines constructors call?
and if use jmockit @mocked instead, there different answers of questions above?
thank you!
mock objects created mockito don't call constructor or static initializer. (this achieved through objenesis in older versions of mockito, , bytebuddy in newer versions.) consequently, of fields uninitialized, , no side effects in constructors happen @ including exceptions might see thrown.
in contrast, spy objects do have constructors called. mockito default calling no-argument constructor (public or private) if don't initialize field, , can call constructor of choice inside initializer.
the order of @mock annotation initialization depends on technique use initialize mocks:
- if use
mockitojunitrunner
, mocks initialized after initializer blocks, constructors, , @rules, , before other @befores defined in blockjunit4classrunner. - if use
mockitorule
, mocks initialized before @before methods, in undefined order compared other @rules unless chain them manuallyrulechain
. - if use
mockitoannotations.initmocks()
, mocks initialized when call method, after initializer blocks , rules, , (if call within @before method) in undefined order compared other @before methods.
Comments
Post a Comment