我们与调度弹簧启动项目,该项目以固定间隔从数据库中读取数据。
虽然使用Maven构建从STS项目,我们同时运行测试用例即使最终构建状态是成功获得下面的错误在控制台中。
org.springframework.beans.factory.BeanCreationNotAllowedException:错误创建名称为豆“的entityManagerFactory”:辛格尔顿bean创建时不允许使用这家工厂的单身人士都在破坏(不要求从Bean工厂的破坏方法实现豆!)在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216)在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)在org.springframework.beans.factory。 support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:523)在org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java: 276)在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTra nslationInterceptor.java:162)在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:145)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)在org.springframework.data .jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (ExposeInvocationInterceptor.java:92)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)在的com.sun。代理。$ Proxy70.findByTraIdAndTransactionNameAndExecutionTime(来源不明)在
应用程序文件
@SpringBootApplication
@PropertySource("classpath:application.properties")
@EnableScheduling
public class ProvisioningApplication {
public static void main(String[] args) {
SpringApplication.run(ProvisioningApplication.class, args);
}
}
计划文件
的BusinessService具有读取数据库中的逻辑
@Component
public class SchedulerJob {
@Autowired
BusinessService service;
@Scheduled(fixedRate=300000) //5mnts
public void schdeule() {
service.startService();
}
}
测试文件
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProvisioningApplication.class)
public class ProvisioningApplicationTests {
@Test
public void contextLoads() {
}
}
这里的问题是为什么春天开机运行调度的任务,而建设项目,以及为什么它抛出上述异常?
在春天开机,当你做一个Maven构建测试用例默认情况下运行。在这种情况下集成测试脚本运行的将尝试连接到您的数据库。既然你没有什么可excecuted作为您的项目集成测试的一部分。一个可能的解决方案是申报类ProvisioningApplicationTests为抽象。这将限制实例创建ProvisioningApplicationTests类的。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProvisioningApplication.class)
public abstract class ProvisioningApplicationTests {
@Test
public void contextLoads() {
}
}
另一种方法来解决这个问题,包括在你的pom.xml下面的代码
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<skipTests>true</skipTests>
<includes>
<include>**/*IT.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
这样就可以排除被执行您的集成测试类,同时建立youy项目。 Maven的万无一失,插件是用来运行单元测试。 Maven的故障保护,插件是用来运行集成测试。虽然使用这种方法确保所有的集成类文件名以“IT”结束。例如。 UserTestIT.java