Spring 重试@Retryable 不重试也不恢复
Spring Retry @Retryable not retrying nor recovering
当我 运行 我的单元测试时,我希望方法 thisFails() 重试 3 次,然后我希望看到打印的恢复记录器行,但它只尝试一次然后抛出异常.底部的输出是在我 运行 我的测试之后。
我错过了什么?
请忽略此部分,跳转到代码部分。 linter 认为我没有足够的说明来post。我认为措辞足以让我的问题得到理解,但出于某种原因,除非我写更多的东西,否则我不允许 post 这个问题。所以这里有更多的东西,等等。
--Spring 引导应用程序--
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;
}
}
--测试Class--
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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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
Spring 重试需要 Spring ApplicationContext;您正在使用 Mockito 的 @InjectMocks
而不是 Spring 的 @Autowired
,以及 SpringJunit4ClassRunner
(或更新的 SpringRUnner
)@RunWith
.
因为没有 ApplicationContext
测试,所以没有重试。
当我 运行 我的单元测试时,我希望方法 thisFails() 重试 3 次,然后我希望看到打印的恢复记录器行,但它只尝试一次然后抛出异常.底部的输出是在我 运行 我的测试之后。
我错过了什么?
请忽略此部分,跳转到代码部分。 linter 认为我没有足够的说明来post。我认为措辞足以让我的问题得到理解,但出于某种原因,除非我写更多的东西,否则我不允许 post 这个问题。所以这里有更多的东西,等等。
--Spring 引导应用程序--
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;
}
}
--测试Class--
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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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
Spring 重试需要 Spring ApplicationContext;您正在使用 Mockito 的 @InjectMocks
而不是 Spring 的 @Autowired
,以及 SpringJunit4ClassRunner
(或更新的 SpringRUnner
)@RunWith
.
因为没有 ApplicationContext
测试,所以没有重试。