Skip to content

@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

Closed
dome313 opened this issue Mar 3, 2017 · 5 comments
Closed
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@dome313
Copy link

dome313 commented Mar 3, 2017

The Spring boot configuration I'm using is

@SpringBootApplication
@EnableScheduling
public class Application {
  public static void main(final String[] args) { SpringApplication.run(Application.class, args);}
}

In configuration there's a bean

@Component
public class MyBeanWithScheduledAnnotation {
  @Scheduled(fixedRateString = '#{fixedRate}')
  public void myJob() {
    ....
  }
}

If I use this bean with @SpyBean in my SpringBootTest, the scheduled job does not execute. The test code looks like this:

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test

@SpyBean
MyBeanWithScheduledAnnotation bean
...

pom.xml contains only two dependencies spring-boot-starter-web and spring-boot-starter-test

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 3, 2017
@wilkinsona
Copy link
Member

The underlying problem here is that ScheduledAnnotationBeanPostProcessor is called to post-process MyBeanWithScheduledAnnotation with the Mockito-created proxy. AopUtils.getTargetClass(Object) returns the proxy class and, as a result, the @Scheduled-annotated method isn't found.

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.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Mar 17, 2017
@philwebb
Copy link
Member

I can't think of an easy way to fix this. I'm a little surprised that ScheduledAnnotationBeanPostProcessor doesn't pick up inherited @Scheduled methods.

@philwebb
Copy link
Member

@jhoeller is not picking up inherited @Scheduled methods an intentional design decision? I imagine we've got something like this:

public class MyBeanWithScheduledAnnotation {
    
    @Scheduled(fixedRateString = '#{fixedRate}')
    public void myJob() {
        ....
    }
}

public class MockitoCGLIBProxy extends MyBeanWithScheduledAnnotation {

    public void myJob() {
        // spy logic
        realBean.myJob();
        // spy logic
    }

}

@philwebb philwebb added priority: low type: bug A general bug and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Mar 31, 2017
@dome313
Copy link
Author

dome313 commented Apr 6, 2017

@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:

@Component
public class StorageBean {
  public void manualSave() {
   ....
  }

  @Scheduled(fixedRateString = '#{fixedRate}')
  public void autoSave() {
    ....
  }
}

and then in test

verifyZeroInteractions(storageBean).autoSave()
// or
verify(storageBean).autoSave()

I know that are different approaches to test such functionality, this is just where I spotted the issue.

@philwebb
Copy link
Member

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.

@philwebb philwebb added status: declined A suggestion or change that we don't feel we should currently apply and removed priority: low type: bug A general bug labels Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants