Java 在Junit测试中禁用Spring@EnableScheduling

Java 在Junit测试中禁用Spring@EnableScheduling,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,我想在Spring测试中禁用@Schedule,但我找不到方法 我曾尝试为测试环境创建不同的配置类,但仍然会触发任务。 这是配置: @Configuration @EnableTransactionManagement @EnableScheduling @ComponentScan({"de.package"}) @PropertySource(name="application.properties", value="classpath:application.properties") pu

我想在Spring测试中禁用@Schedule,但我找不到方法

我曾尝试为测试环境创建不同的配置类,但仍然会触发任务。 这是配置:

@Configuration
@EnableTransactionManagement
@EnableScheduling
@ComponentScan({"de.package"})
@PropertySource(name="application.properties", value="classpath:application.properties")
public class PersistenceJPAConfig {
...
}
这是test-emvironment配置。刚刚删除了@EnableScheduling注释

@Configuration
@EnableTransactionManagement
@ComponentScan({"de.package"})
@PropertySource(name="application.properties", value="classpath:application.properties")
public class PersistenceJPATestConfig {
...
}
在测试中,我使用:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceJPATestConfig.class }, loader = AnnotationConfigContextLoader.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class GetArticlesTest {
...
}

但是当我运行测试时,任务仍然被激发..有什么方法可以在运行测试时停止执行任务吗

当您同时在同一个包上使用@ComponentScan时,spring似乎也在加载另一个配置

您可以使用一些配置文件来过滤这些内容,比如在PersistenceJPatesConfig中添加这些内容

@Profile("test")
在JUnit类上添加此注释,以便使用“测试”概要文件执行它

编辑:
您的主配置文件也应该被分析,以便在其配置文件不处于活动状态时忽略它,因此您应该在主配置文件类上添加另一个@profile,该配置文件与“test”不同(对于spring boot用户而言),只需将下面的代码添加到主配置中,这样它就不会在测试配置文件和测试用例上运行了!没有其他变化

@Profile(!test)
public class Config{
......
}

这个问题有多个答案不同的重复项。在我看来,这是唯一有效的。
@Profile(!test)
public class Config{
......
}