Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BeanCreationNotAllowedException在SpringBoot项目中的junit测试之后_Java_Spring Boot_Junit_Spring Cloud - Fatal编程技术网

Java BeanCreationNotAllowedException在SpringBoot项目中的junit测试之后

Java BeanCreationNotAllowedException在SpringBoot项目中的junit测试之后,java,spring-boot,junit,spring-cloud,Java,Spring Boot,Junit,Spring Cloud,我有一个Spring Boot项目。 它可以成功启动。 我添加了这样一个测试: @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = SopStart.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK) public class Test{ @Test public void test(){

我有一个Spring Boot项目。
它可以成功启动。
我添加了这样一个测试:

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SopStart.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class Test{
    @Test
    public void test(){
        System.out.println("success!");
    }
}
“成功!”可以成功打印。但最后我得到了一个例外:

2017-10-25 17:00:42.481  INFO [bootstrap,,,] 7280 --- [      Thread-15] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@758d0555: startup date [Wed Oct 25 17:00:09 CST 2017]; parent: org.springframework.web.context.support.GenericWebApplicationContext@75d4a80f
2017-10-25 17:00:42.492  WARN [bootstrap,,,] 7280 --- [      Thread-15] s.c.a.AnnotationConfigApplicationContext : Exception thrown from ApplicationListener handling ContextClosedEvent

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    ...

它正在拾取默认的
应用程序
类。尝试为测试类定义您的WIN配置,该测试类不包括某些自动配置的bean,包括
EurekaClientAutoConfiguration
,这是您的测试抱怨的主要原因

您可能也不需要
@WebAppConfiguration
。您甚至可能会得到一个
java.lang.IllegalStateException

举个例子,

@ActiveProfiles("test")
//@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestExample.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = {Test.CustomConfiguration.class})
public class Test {

    @Configuration
    @EnableConfigurationProperties
    @EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class,
            JpaRepositoriesAutoConfiguration.class,
            DataSourceAutoConfiguration.class,  
            EurekaClientAutoConfiguration.class})
    static class CustomConfiguration {

    }

    @Test
    public void test() {
        System.out.println("success!");
    }
}