如何在Spring Boot IntegrationTest上禁用计划自动启动?
谢谢.
我正在使用Maven开发SpringBoot应用程序.
我有一个带有@Component注释的类,它有一个m带@Scheduled(initialDelay = 1000, fixedDelay = 5000)注释的方法.这里fixedDelay可以设置为指定从完成任务开始测量的调用之间的间隔.
我还在@EnableScheduling主类中注释:
@SpringBootApplication
@EnableScheduling
public class FieldProjectApplication {
public static void main(String[] args) {
SpringApplication.run(FieldProjectApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
现在每当我运行测试时,定义为:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BankitCrawlerTests {
...
}
Run Code Online (Sandbox Code Playgroud)
计划任务m也每5秒运行一次.
当然,我只想在应用程序运行时运行计划任务.我该怎么做(即阻止计划任务在运行测试时运行)?
当我通过JUnit Test Case执行代码时,我收到以下异常
org.springframework.beans.factory.BeanCreationNotAllowedException:创建名为'somarFactory'的bean时出错:当这个工厂的单例处于销毁状态时不允许使用单例bean创建(不要在destroy方法实现中从BeanFactory请求bean!)
有人可以建议可能出现的问题吗?
谢谢,凯西尔
我想将一个Spring注入一个BeanFactoryBean创建的同样BeanFactory
是以任何方式这样做的吗?
顺便说一句,我正在开发一个Web应用程序.如果不是,我知道我可以获得BeanFactory,RequestContext但我想要注入的bean BeanFactory不在requestContext但仍在应用程序上下文中.我能这样做吗?
我想在Spring测试中禁用@Schedule,但我找不到办法.
我试图为测试环境制作不同的配置类,但仍然会触发任务.这是配置:
@Configuration
@EnableTransactionManagement
@EnableScheduling
@ComponentScan({"de.package"})
@PropertySource(name="application.properties", value="classpath:application.properties")
public class PersistenceJPAConfig {
...
}
Run Code Online (Sandbox Code Playgroud)
这是测试环境配置.刚删除@EnableScheduling注释
@Configuration
@EnableTransactionManagement
@ComponentScan({"de.package"})
@PropertySource(name="application.properties", value="classpath:application.properties")
public class PersistenceJPATestConfig {
...
}
Run Code Online (Sandbox Code Playgroud)
在测试我使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPATestConfig.class }, loader = AnnotationConfigContextLoader.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class GetArticlesTest {
...
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行测试时,任务仍然被解雇..有没有办法在运行测试时停止执行任务?