Fixes #2648 : Add support for customising strictness via @Mock annotation and MockSettings by sivaprasadreddy · Pull Request #2650 · mockito/mockito (original) (raw)
This was referenced
May 31, 2022
ijuma pushed a commit to apache/kafka that referenced this pull request
Changes
- mockito: 4.4.0 -> 4.6.1 (https://github.com/mockito/mockito/releases)
Most important updates:
- Fixes mockito/mockito#2648 : Add support for customising strictness via @mock annotation and MockSettings mockito/mockito#2650
Why is this change needed?
According to the Mockito documentation :
Although it is possible to verify a stubbed invocation, usually it's just redundant. Let's say you've stubbed foo.bar(). If your code cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what get(0) returns then it should not be stubbed.
While working on the Replace EasyMock and PowerMock with Mockito for StreamsMetricsImplTest I noticed that described behavior wasn't applied when you create a new mock like this.
final Metrics metrics = mock(Metrics.class);
when(metrics.metric(metricName)).thenReturn(null);
... invoke SUT
verify(metrics).metric(metricName); // this should be redundant (according to docs)
After further investigation I figured out that described behaviour wasn't implemented untilv4.6.1.
With this change we are now able to mock objects like this:
Foo explicitStrictMock = mock(Foo.class, withSettings().strictness(Strictness.STRICT_STUBS));- link to docs: MockSettings.html#strictness
It looks like I can accomplish the same thing by using the @RunWith(MockitoJUnitRunner.StrictStubs.class) instead of the @RunWith(MockitoJUnitRunner.class) so mockito dependency version update is not mandatory, but it would be nice to stay up-to-date and use the latest version (it's up to MR reviewer to decide if we are going to merge this now, or just close the MR and update mockito version later).
Reviewers: Ismael Juma ismael@juma.me.uk
a0x8o added a commit to a0x8o/kafka that referenced this pull request
Changes
- mockito: 4.4.0 -> 4.6.1 (https://github.com/mockito/mockito/releases)
Most important updates:
- Fixes mockito/mockito#2648 : Add support for customising strictness via @mock annotation and MockSettings mockito/mockito#2650
Why is this change needed?
According to the Mockito documentation :
Although it is possible to verify a stubbed invocation, usually it's just redundant. Let's say you've stubbed foo.bar(). If your code cares what foo.bar() returns then something else breaks(often before even verify() gets executed). If your code doesn't care what get(0) returns then it should not be stubbed.
While working on the Replace EasyMock and PowerMock with Mockito for StreamsMetricsImplTest I noticed that described behavior wasn't applied when you create a new mock like this.
final Metrics metrics = mock(Metrics.class);
when(metrics.metric(metricName)).thenReturn(null);
... invoke SUT
verify(metrics).metric(metricName); // this should be redundant (according to docs)
After further investigation I figured out that described behaviour wasn't implemented untilv4.6.1.
With this change we are now able to mock objects like this:
Foo explicitStrictMock = mock(Foo.class, withSettings().strictness(Strictness.STRICT_STUBS));- link to docs: MockSettings.html#strictness
It looks like I can accomplish the same thing by using the @RunWith(MockitoJUnitRunner.StrictStubs.class) instead of the @RunWith(MockitoJUnitRunner.class) so mockito dependency version update is not mandatory, but it would be nice to stay up-to-date and use the latest version (it's up to MR reviewer to decide if we are going to merge this now, or just close the MR and update mockito version later).
Reviewers: Ismael Juma ismael@juma.me.uk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})