java - Mockito InvocationImpl retained across TestSuite tests -
i have junit test suite ~800 tests. of these wired spring , large number use mockito mock/spy behavior. started running out of memory errors. while analyzing hprof dump noticed > 30% of heap consumed mockito invocationimpls retained between tests.
is there way clear these after test class completes? not want use mockito.reset( mock ) mocks initialization varies each test. if not, appears need split tests accommodate leak.
from this link appears mockito team recognizes these retained trade-off of verification after execution approach. i'm wondering if has found means clear these large number of unit tests can strung in suite.
i found partial work around. in case vast majority of invocationimpl
instances being created during single test case used spy()
create real partial mock 1 method overridden. using mockito 1.10.19, switched partial mock construction spy()
mock( <class>, withsettings().spiedinstance( realinstance ).defaultanswer( calls_read_mathods ).stubonly() )
.
while prevents use of verify()
on mock, stubonly()
keeps mock storing instances of invocationimpl
each call made spy during test, , decreased heap use.
since mocks created springockito in spring context files, needed ugly rework of bean definition match mock()
call, follows. allows spy used in spring-wired domain classes.
<bean id="realinstance" class="<real instance class>" /> <bean id="instspysettings" class="org.mockito.mockito" factory-method="withsettings" /> <bean id="instspypartialmock" factory-bean="instspysettings" factory-method="spiedinstance"> <constructor-arg> <ref local="realinstance" /> </constructor-arg> </bean> <bean id="instspydefaultanswers" factory-bean="instspypartialmock" factory-method="defaultanswer"> <constructor-arg><util:constant static-field="org.mockito.mockito.calls_real_methods"/></constructor-arg> </bean> <bean id="instspystubonly" factory-bean="instspydefaultanswers" factory-method="stubonly" /> <bean id="spyinstance" class="org.mockito.mockito" factory-method="mock"> <constructor-arg value="<real instance class>" /> <constructor-arg> <ref local="instspystubonly" /> </constructor-arg> </bean>
Comments
Post a Comment