Spring Retry @Retryable不会重试也不会恢复

时间:2017-09-27 20:25:07

标签: java spring spring-boot recover spring-retry

当我运行单元测试时,我希望方法thisFails()重试3次然后我希望看到恢复记录器行打印,但它只尝试一次然后抛出异常。底部的输出是在我运行测试之后。

我错过了什么?

随意忽略此部分,然后跳转到代码。短信认为我没有足够的说明才能发布。我认为这个措辞足以让我的问题得到解决,但由于某种原因,我不允许发布这个问题,除非我写更多的东西。所以这里有更多的东西,等等等等。

- Spring Boot Application -

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;

@EnableRetry
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

- 服务 -

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MyService {

  @Retryable(include = RuntimeException.class)
  public int thisFails() {
    log.info("Help I am failing");
    throw new RuntimeException();

  }
  @Recover
  public int thisRecovers(RuntimeException re) {
    log.info("I recovered");
    return 0;
  }
}

- 测试类 -

package com.example.demo;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {
  @InjectMocks
  MyService service;

  @Test
  public void recovery(){
    service.thisFails();
  }

}

输出

Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x10983b4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1099034e0). One of the two will be used. Which one is undefined.
12:58:32.067 [main] INFO com.example.demo.MyService - Help I am failing

java.lang.RuntimeException
    at com.example.demo.MyService.thisFails(MyService.java:15)
    at com.example.demo.MyServiceTest.recovery(MyServiceTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code 255

1 个答案:

答案 0 :(得分:4)

Spring Retry需要Spring ApplicationContext;您使用的是Mockito的@InjectMocks而不是Spring的@Autowired,以及SpringJunit4ClassRunner(或较新的SpringRUnner@RunWith

由于测试没有ApplicationContext,因此无法重试。