You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working with Mockito and using MockitoJUnitRunner combined with @Mock/@Spy and friends there is quite a performance impact.
When using @Mock each time an @Test method is encountered a new instance of the mock is being created. (same applies to @Spy). I've rewritten the test to use Mockito.mock in an @BeforeClass and use Mockito.reset in an @Before method and performance of the tests went up considerably.
Resulting from 50% to 80% in some cases. (From 1s to about 245ms for instance). Apparently mock creation is a heavy operation.
Using some form of caching in the testing infrastructure would/could be beneficial in improving the performance of using @Mock and friends.
The text was updated successfully, but these errors were encountered:
Hi, this is an interesting idea, however I wouldn't make this a default behavior because you never know if production code caches these objects somewhere and perform reference equality. While this is probably not the case in any sane unit test and production code, it is not something we can rely on.
On top of that you cannot assume the test methods won't run concurrently (maven|gradle).
Did you narrowed it down to objenesis strategy ? Also Mockito now has an org.mockito.plugins.InstantiatorProvider plugin that allows to provide his own mock Instantiator although this doesn't allow to access the mock creation (i.e. the test class).
Actually Spring Boot does something similar and reuse mocked beans/instances and resets them before the test. So it appears to work.
I was thinking something along the lines of caching/reuse it on a per test class basis or provide a plugable strategy of some sorts. I didn't investigate further where the time was actually spend.
When working with Mockito and using
MockitoJUnitRunner
combined with@Mock
/@Spy
and friends there is quite a performance impact.When using
@Mock
each time an@Test
method is encountered a new instance of the mock is being created. (same applies to@Spy
). I've rewritten the test to useMockito.mock
in an@BeforeClass
and useMockito.reset
in an@Before
method and performance of the tests went up considerably.Resulting from 50% to 80% in some cases. (From 1s to about 245ms for instance). Apparently mock creation is a heavy operation.
Using some form of caching in the testing infrastructure would/could be beneficial in improving the performance of using
@Mock
and friends.The text was updated successfully, but these errors were encountered: