-
Notifications
You must be signed in to change notification settings - Fork 41.2k
@Scheduled annotation does not work in test if bean is annotated with @SpyBean #8489
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
Comments
The underlying problem here is that It is intentional that the spy is created first (see #5837 and cdfbf28) so I'm not sure what we can do here. Any ideas, @philwebb? @dome313 As an aside, why do you want to spy on a scheduled bean? It seems like an unusual thing to be trying to do. |
I can't think of an easy way to fix this. I'm a little surprised that |
@jhoeller is not picking up inherited public class MyBeanWithScheduledAnnotation {
@Scheduled(fixedRateString = '#{fixedRate}')
public void myJob() {
....
}
}
public class MockitoCGLIBProxy extends MyBeanWithScheduledAnnotation {
public void myJob() {
// spy logic
realBean.myJob();
// spy logic
}
} |
@wilkinsona my test case scenario is following: there's a storage bean which can do manual and auto (with Scheduled annotation) save. In the test case I want to verify that the records are saved and if autoSave method was actually called or not, that's why I'm using Spy. Class is like:
and then in test
I know that are different approaches to test such functionality, this is just where I spotted the issue. |
Unfortunately I don't think there's an easy way for us to fix this. I'm going to close it for now and suggest that you test in a different way. |
The Spring boot configuration I'm using is
In configuration there's a bean
If I use this bean with
@SpyBean
in mySpringBootTest
, the scheduled job does not execute. The test code looks like this:pom.xml contains only two dependencies
spring-boot-starter-web
andspring-boot-starter-test
The text was updated successfully, but these errors were encountered: