Mockito InvocationImpl stored in TestSuite tests

I have a JUnit Test Suite with ~ 800 tests. Most of them are connected using Spring, and a large number use Mockito for bullying / espionage behavior. I have errors in my memory. While analyzing the hprof dump, I noticed that> 30% of the heap is consumed by Mockito InvocationImpls, which are saved between tests.

Is there a way to clear them after the test class finishes? I do not want to use Mockito.reset (mock), since the initialization of mocks depends on each test. If not, it seems I will need to separate the leak tests.

From this link , it seems that the Mockito team acknowledges that they are saved as a compromise between validation after completing the approach. But I wonder if someone found a tool to clean them, so a large number of unit tests can be combined into a Suite.

+4
source share
1 answer

I found a partial job. In my case, the vast majority of instances InvocationImplwere created during one test case, which was used spy()to create a real partial layout, so one method could be overridden. I use Mockito 1.10.19, so I switched the partial layout design from spy()to mock( <class>, withSettings().spiedInstance( realInstance ).defaultAnswer( CALLS_READ_MATHODS ).stubOnly() ).

verify() , stubOnly() mock InvocationImpl , , .

Mocks Springockito Spring, bean mock() . Spring -wired.

  <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>
+2

Source: https://habr.com/ru/post/1649056/


All Articles