【问题标题】:Why use JUnit to test Feign client cases java.lang.IllegalStateException: Shutdown in progress为什么使用 JUnit 测试 Feign 客户端案例 java.lang.IllegalStateException: Shutdown in progress
【发布时间】:2018-07-20 03:45:05
【问题描述】:

我想知道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.

所以我想问为什么会这样,这个错误信息是什么意思?以及如何避免这种情况?

【问题讨论】:

  • 您是否阅读了错误信息?
  • 我不明白你的意思......
  • Exception caught (might be ok if at shutdown)
  • 我注意到这句话,不影响我正常测试。因为我只想做一个简单的单元测试来确保 Feign 是可调用的。但是有没有办法避免日志中出现这个异常消息?就算没有效果。能不能告诉我在相关微服务启动后有没有更好的方法来测试 FeignClient?
  • 由于它是功能区,它可能与请求的线程调度有关,并且在非常短暂的上下文中是可以预期的。最简单的方法是将该记录器的日志级别设置为WARNERROR

标签: java spring-boot junit spring-cloud


【解决方案1】:

我也有同样的问题,但这个例外没什么大不了的。如果你想完成测试,你只需添加逻辑代码重复它。 像这样:

@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) {}
    }
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 2018-06-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2018-04-19
    相关资源
    最近更新 更多