Skip to content

Improve performance of MockitoJUnitRunner #1509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mdeinum opened this issue Oct 3, 2018 · 2 comments
Open

Improve performance of MockitoJUnitRunner #1509

mdeinum opened this issue Oct 3, 2018 · 2 comments

Comments

@mdeinum
Copy link

mdeinum commented Oct 3, 2018

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.

@bric3
Copy link
Contributor

bric3 commented Oct 4, 2018

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).

@mdeinum
Copy link
Author

mdeinum commented Oct 4, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants