jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
462 stars 239 forks source link

withCapture captures calls with generics for similar mocks twice #671

Open StefanHoening opened 4 years ago

StefanHoening commented 4 years ago

Version of JMockit: I can reproduce the issue with JMockit 1.49.

I have a Maven Project as Testcase:

generic-parameter.zip

I have a Base Interface using generics (GenericHandler) with two method, where the "handleList" method is causing the issue. I have 2 derived interfaces of GenericHandler for types A and B: HandlerA and HandlerB.

Inside the testcase I am using Mocks for HandlerA and HandlerB. the method "handleList" is called on both Mocks. But the capturing list provided to the "withCapture" method for the different mocks captures both calls for both mocks.

The problem does not occur with the "handleValue" method of the mocks (see respective testcase).

rliesenfeld commented 4 years ago

Seems like a bug, indeed. It doesn't occur if the tests use @Injectable instead of @Mocked (as they should).

Also, tests like these are over-mocking. If you care about having good tests at all, use mocking only where needed (which means, most good tests will mock nothing at all).

StefanHoening commented 4 years ago

Seems like a bug, indeed. It doesn't occur if the tests use @Injectable instead of @Mocked (as they should).

So I should prefer to use @Injectable when I use the instances in other classes? (I am just confused whether "(as they should)" belongs to @Injectable or to @Mocked) Can you point me to some documentation, which explains in detail, when to use @Injectable and when to use @Mocked?

May be this test is over mocking. I strippd down the test from my original case, where I was using two different derived Spring CrudRepository implementations, which I wanted to mock to be able to check the repositories save operations happen without having the more time consuming spring setup and database accesses.

rliesenfeld commented 4 years ago

The most detailed and easily accessible documentation is the API documentation (assuming you know how to use your Java IDE). It also contains links to relevant sections in the Tutorial.