-
Notifications
You must be signed in to change notification settings - Fork 470
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
MockitoMockMaker provides mocking of static methods #1756
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1756 +/- ##
============================================
+ Coverage 80.44% 81.53% +1.08%
- Complexity 4337 4470 +133
============================================
Files 441 442 +1
Lines 13534 13840 +306
Branches 1707 1735 +28
============================================
+ Hits 10888 11284 +396
+ Misses 2008 1912 -96
- Partials 638 644 +6 ☔ View full report in Codecov by Sentry. |
9f31d01
to
c3a79b7
Compare
c3a79b7
to
4988f39
Compare
4988f39
to
02a435c
Compare
923ee45
to
e1161db
Compare
spock-specs/src/test/groovy/org/spockframework/docs/interaction/StaticMocksDocSpec.groovy
Outdated
Show resolved
Hide resolved
...-specs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoStaticMocksSpec.groovy
Outdated
Show resolved
Hide resolved
...cs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoInlineMockMakerSpec.groovy
Outdated
Show resolved
Hide resolved
spock-core/src/main/resources/org/spockframework/idea/spock.gdsl
Outdated
Show resolved
Hide resolved
...-specs/src/test/groovy/org/spockframework/mock/runtime/mockito/MockitoStaticMocksSpec.groovy
Show resolved
Hide resolved
e1161db
to
af863d5
Compare
@leonard84 As a hint, I needed to push the I also adapted the javadoc on |
I have created an issue in the Mockito repo to request the API for the same inline mockMaker instance, and not relying on private method from Mockito in the method |
155b00a
to
fa28c2e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I'd like some additional feedback from @spockframework/supporter as we are now extending the API in Specification
in a bigger way.
spock-core/src/main/java/org/spockframework/mock/IStaticMockController.java
Outdated
Show resolved
Hide resolved
03c27d4
to
66db1f5
Compare
93ded6f
to
4257692
Compare
A thing that came to mind is, whether we should use given:
Mock(StaticClass, static: true)
when:
def result = StaticClass.staticMethod()
then:
1 * StaticClass.staticMethod() >> MOCK_VALUE
result == MOCK_VALUE |
@leonard84 that would not work as you might expect. The
|
We could just return
Correct me if I'm wrong, but the declarations look indentical, except that you leave out the @Beta
public <T> T Mock(
@DelegatesTo.Target
Class<T> type,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0)
@ClosureParams(FirstParam.FirstGenericType.class)
Closure interactions) {
throw invalidMockCreation();
}
public <T> void MockStatic(
@DelegatesTo.Target
Class<T> type,
@DelegatesTo(strategy = Closure.DELEGATE_FIRST, genericTypeIndex = 0)
Closure<?> interactions) {
throw invalidMockCreation();
} |
I was wondering IIRC the inline mocking of Mockito would actually enable global mocking, as it instruments every class looks up if the current instance is "mocked" so if you return always |
So the |
Theoretically yes, but this would at least require more API on the We could ask the mockito guys, how they would feel about that. But what do you want to accomplish with that? |
You are mistaken, if you leave out the explicit param it will use MockStatic(StaticClass) {
staticMethod() >> "MockValue"
} will be compiled to this.MockStaticImpl(null, null, StaticClass, { ->
this.getSpecificationContext().getMockController().addInteraction(new org.spockframework.mock.runtime.InteractionBuilder(3, 7, 'staticMethod() >> \"MockValue\"').addEqualTarget(it).addEqualMethodName('staticMethod').setArgListKind(true, false).addConstantResponse('MockValue').build())
}) it doesn't use the delegate but The closure parameter is only used to rename MockStatic(StaticClass) { sc ->
sc.staticMethod() >> "MockValue"
} this.MockStaticImpl(null, null, StaticClass, { java.lang.Object sc ->
this.getSpecificationContext().getMockController().addInteraction(new org.spockframework.mock.runtime.InteractionBuilder(3, 7, 'sc.staticMethod() >> \"MockValue\"').addEqualTarget(sc).addEqualMethodName('staticMethod').setArgListKind(true, false).addConstantResponse('MockValue').build())
}) and here |
Well, the same as in Groovy, it intercept things that you could't create and inject yourself. |
Yes You can see the runtime resolution in the class Maybe in general: I find the behaviour and user expectation of static mocks different enough to justify the new |
I think they are comparable to |
Also renamed all methods accordingly.
I will change the API to onyl provide: * instead of the |
Added test to verify interaction is reset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry that I didn't have time to have a look at this earlier.
I had a cursory look now (mainly at the docs) and generally quite like it.
Some points though:
- It seems there used to be
StubStatic
andMockStatic
too. I actually wonder why they were removed and there is also no "A la carte" way to create the mock for example with a different or customdefaultResponse
. Is that intentional, or can we add this? I mean, you can of course easily useStaticClass._ >> { ZeroOrNullResponse.INSTANCE.respond(delegate) }
andStaticClass._ >> _
to achieve the same, it is just not as convenient. :-) - "Used to be there" I assumed, because it is still in the docs in the mockito mock maker section: "
Mock/Stub/SpyStatic
", and also the plural form in "The semantics are the same as for the non-static variants:", and the plural form in "The static mock methods will". - The whole documentation of the feature is extremely unlucky, because the "Mocking Static Methods" chapter is a sub-chapter of "Groovy Mocks" as you just extended the section about global Groovy mocks. This has to be split or at least moved. I'm not sure whereto, but probably to "Other Kinds of Mock Objects". If you look at the all-in-one page, you can quite nicely see the overall structure on the left.
And the link in the release notes on the all-in-one page is broken |
@Vampire I will open up a new PR to fix the documentation issues.
Yes there were also And yes you ca still do that as you decribed with the interaction, so there is also a work around, I will document that. |
MockitoMockMaker supports to mock static methods
of Java classes with the new Spec API:
SpyStatic(Class)
SpyStatic(Class, IMockMakerSettings)
This also supports Java callers of the static methods.
Usage sample: