Good morning Engine developers,
As of this morning, oVirt Engine has migrated to the next generation
of Mockito, specifically, the latest available version - 2.6.5 (well,
the latest version at the time this email was issued. Their z-stream
are as small as a single patch, and thus REALLY fast).
First of all - a big shout-out to everyone who took the time to review
and comment on this patch series.
Why did we do this?
1. The Mockito 1.x generation is no longer supported, and we want to
work with a live, maintained, library.
2. Support for Java 8 syntax.
3. Improved generics support, lending to cleaner code.
4. Improved performance.
Unfortunately(?), the Mockito 2.x generation isn't 100% backwards
compatible. Here are the main changes you need to know about.
**"Sneaky" Behavior Changes:**
1. In Mockito 1.x, stubbing a mock and then not using this recorded
behavior was not an issue (besides the fact that you're probably
writing unused code :-)). In Mokcito 2.x, by default, having unused
stubbing will fail your test. Usually, the best course of action is to
remove these useless stubbings (as I've done for most of the
occurrences in the code). If this isn't easily possible (e.g., these
stubbings are inherited from a base class), or if removing them would
actually make the test less easy to follow, you can use the
org.mockito.junit.MockitoJUnitRunner.Silent runner to revert to
Mockito 1.x's behavior.
2. In Mockito 1.x, any(Class) also matches nulls (e.g.,
any(SomeClass.class) would match any instance of SomeClass or null).
In Mockito 2.x, any(Class) *no longer matches nulls*. In order to also
match nulls you could use any() or nullable(Class).
**Code Changes**
1. Mockito 2.x's ArgumentMatcher no longer extend Hamcrest's Matcher,
and was in fact converted to a [functional] interface. Beyond the
simple change of using "implements" instead of "extends", this also
means you can write much cleaner code and have your matchers as
anonymous lambdas. If you still want to use a Hamcrest matcher for
your stubbing, you can use
org.mockito.hamcrest.MockitoHamcrest#argThat.
2. anyListOf/anySetOf/anyCollectionOf/anyMapOf are now deprecated and
should not be used (well, at least mostly - see below). Their
counterparts, anyList/anySet/anyCollection/anyMap no longer return raw
types, but generalized ones. Utilizing Java 8's type inference,
Mockito 2.x allows you to use the simpler versions without getting
unchecked assignments warnings. Note that this capability depends on
Java's language level, so frontend projects that still use Java 7 will
have to use the deprecated versions, unfortunately. I've already
applied this change to the entire backend, but you should note it when
writing new patches.
3. anyObject() is deprecated - just use any() instead. I've already
applied this change to the entire project, but you should note it when
writing new patches.
4. org.mockito.runners.MockitoJUnitRunner has been hollowed out and
deprecated. Use org.mockito.junit.MockitoJUnitRunner (or
org.mockito.junit.MockitoJUnitRunner.Silent, as explained above)
instead. I've already applied this change to the entire project, but
you should note it when writing new patches.
5. org.mockito.Matchers has been hollowed out and deprecated. Use
org.mockito.ArgumentMatchers instead. I've already applied this change
to the entire project, but you should note it when writing new
patches.
Additional information is available on Mockito's website [1], and
specifically the "What's new in Mockito 2" page [2].
If you have any other questions, feel free to reply to this thread or
contact me directly.
Have a great mocking day,
Allon
[1]
http://site.mockito.org/
[2]
https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2