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
Mocking action declaration may not take effect(failed to match) when spring adds a proxy for the mock object #758
Comments
@djj0809 you can unwrap the proxy yourself see (http://amgohan.agileasoft.com/how-to-acess-target-object-behind-a-spring-proxy/). public static <T> T getTargetObject(Object proxy) throws Exception {
if (AopUtils.isJdkDynamicProxy(proxy)) {
return (T) ((Advised) proxy).getTargetSource().getTarget();
} else {
return (T) proxy;
}
} def "sample service test"() {
given:
def sampleRepositryMock = getTargetObject(sampleRepositry)
when:
sampleService.doSomething() // simply invoke sampleRepositry.doSomething() in it
then:
1 * sampleRepositryMock.doSomething()
0 * _
} As for a solution in spock, I'm not sure how to go about this, since the mocking code is part of spock-core which has no knowledge of spring. spock-spring on the other hand does not have access to this part of the code. |
@leonard84 Thanks for your help, your unwrap solution works fine!
|
|
Leonard, thank you for the information. This helped me, but in a slightly different way than how you described. The mock was proxied by cglib, so instead of using AopUtils.isJdkDynamicProxy(proxy), I used AopUtils.isCglibProxy(proxy). Depending on the situation, others might need to use AopUtils.isAopProxy(proxy). Thank you for the suggestion! |
Actually |
#796 adds |
@leonard84 we're desperate to use this feature. When can we expect it to be released? |
@radarsh Spock 1.2-RC2 has been released recently |
Issue description
Mocking action declaration may not take effect when:
@EnableTransactionManagement
is added in the unit test's Spring context(i.e. in other context config classes)@Transactional
annotations in class level or some methods declaration (A typical scenario is using Spring-data-jpa's custom repository interface, we may add@Transactional
to declare related config)Above settings will cause Spring adding proxy for the spock generated mock object, which cause the problem.
See sample code if necessary.
After some digging, I found Something in org.spockframework.mock.runtime.MockObject line 95 :
Is there a way that I can get rid of this problem(A PR or anything else)? Thanks for any attention or help!
How to reproduce
here is a piece of sample code:
The text was updated successfully, but these errors were encountered: