为什么使用JUnit测试Feign客户案例java.lang.IllegalStateException:正在关闭

时间:2018-07-20 03:45:06

标签: java spring-boot junit spring-cloud

我想知道Feign接口是否正确,所以我使用JUnit对其进行测试。 (Spring Boot版本为1.5.13.RELEASE)

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class ProductAuthClientTest {
    @Autowired
    private ProductAuthClient productAuthClient;
    @Test
    public void test(){
      ProductAuth productAuth = productAuthClient.getProductAuth(1);
        Assert.assertNotNull(productAuth);
    }
}

并且JUnit测试通过,但是控制台显示一条错误消息。

2018-07-20 11:32:16.808  INFO 3272 --- [       Thread-5] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook removed for: NFLoadBalancer-PingTimer-service-cloud-mysql
2018-07-20 11:32:16.812  INFO 3272 --- [       Thread-5] c.n.u.concurrent.ShutdownEnabledTimer    : Exception caught (might be ok if at shutdown)

java.lang.IllegalStateException: Shutdown in progress
    at java.lang.ApplicationShutdownHooks.remove(ApplicationShutdownHooks.java:82) ~[na:1.8.0_171]
    at java.lang.Runtime.removeShutdownHook(Runtime.java:239) ~[na:1.8.0_171]
    at com.netflix.util.concurrent.ShutdownEnabledTimer.cancel(ShutdownEnabledTimer.java:70) ~[netflix-commons-util-0.1.1.jar:na]
    at com.netflix.loadbalancer.BaseLoadBalancer.cancelPingTask(BaseLoadBalancer.java:613) [ribbon-loadbalancer-2.2.5.jar:2.2.5]
    at com.netflix.loadbalancer.BaseLoadBalancer.shutdown(BaseLoadBalancer.java:864) [ribbon-loadbalancer-2.2.5.jar:2.2.5]
    at com.netflix.loadbalancer.DynamicServerListLoadBalancer.shutdown(DynamicServerListLoadBalancer.java:285) [ribbon-loadbalancer-2.2.5.jar:2.2.5]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:364) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:287) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:583) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:555) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:516) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1032) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1008) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:958) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.cloud.context.named.NamedContextFactory.destroy(NamedContextFactory.java:76) [spring-cloud-context-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:272) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:583) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:555) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:516) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1032) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1008) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext$2.run(AbstractApplicationContext.java:929) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]

2018-07-20 11:32:16.821  INFO 3272 --- [       Thread-5] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2018-07-20 11:32:16.831  INFO 3272 --- [       Thread-5] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
2018-07-20 11:32:16.856  WARN 3272 --- [       Thread-5] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2018-07-20 11:32:16.856  INFO 3272 --- [       Thread-5] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.

所以我想问为什么会发生此错误消息是什么意思?以及如何避免这种情况?

1 个答案:

答案 0 :(得分:0)

我有同样的问题,但是这个例外并不重要。如果要完成测试,只需添加逻辑代码即可重复进行。 像这样:

@Test
public void test(){
    try{
       ProductAuth productAuth = productAuthClient.getProductAuth(1);
       Assert.assertNotNull(productAuth);
    } catch (IllegalStateException illEx) {
       ProductAuth productAuth = productAuthClient.getProductAuth(1);
       Assert.assertNotNull(productAuth);
    }
}

或者:

@Test
public void test(){
    int num = 10;// try to do 10 times
    while(num > 0) {
         num --;
         try {
             ProductAuth productAuth = productAuthClient.getProductAuth(1);
             Assert.assertNotNull(productAuth);
             break;// if don't have throw exception, than stop this while
         } catch (IllegalStateException illEx) {}
    }
}

希望对您有帮助