当前位置: 首页 > 知识库问答 >
问题:

Spring Retry@Retryable不重试也不恢复

汪凌
2023-03-14

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

我错过了什么?

请忽略此部分,然后跳到代码。门楣匠认为我没有足够的说明来张贴。我认为这样的措辞足以让我的问题被人理解,但出于某种原因,我不允许发布这个问题,除非我写更多的东西。还有更多的东西,等等。

--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;
  }
}

--测试班--

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个答案

公羊嘉
2023-03-14

Spring重试需要Spring ApplicationContext;您正在使用Mockito的@InjectMocks而不是Spring的@Autowired,以及SpringJunit4ClassRunner(或更新的SpringRUnner@RunWith

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

 类似资料:
  • 我从吉图卜克隆了项目。我的解决方案有 6 个项目,每个项目都有 packages.config 文件(其中包含包及其版本的列表),但这些包不会还原。 我已经尝试恢复nuget包的方法: 在 Visual Studio 中:

  • 希望有人能帮我找出,如果不是一个解决方案,至少是一个行为的解释。 问题: 在一些设备上,按下启动器图标会导致当前任务恢复,在其他设备上,会导致初始启动意图被激发(有效地重新启动应用程序)。为什么会这样? 细节: 当您按下“启动程序图标”时,应用程序会正常启动-也就是说,我假设,使用您的第一个和操作和类别。然而,情况并非总是如此: 在大多数设备上,如果您在应用程序已经运行后按下启动器图标,则会恢复该

  • 正如SonarSource所说: 捕获Throwable或Error也将捕获OutOfMemoryError和InternalError,应用程序不应试图从中恢复。 现在,如果在一个事务中,我不应该尝试回滚它吗?或者我会在数据库中造成更糟糕的状态吗?是否在创建OutOfMemoryError时释放了资源,以便我可以再次使用内存?

  • 我有一个ViewPager+FragmentPageAdapter的问题。 场景:我有一个活动在片段a的内部,片段a有一个ViewPager,片段B1,e片段B2。B1 e B2是同一个类,但作为参数的数据不同。 FragmentPagerAdapter 片段B

  • 恢复测试通常可以用来监控训练与恢复之间的平衡度。您可以跟踪身体对训练的反应。除了训练引起的变化外,还有许多其他因素会影响您的恢复测试结果,例如精神压力、睡眠、潜伏疾病和环境变化(温度、海拔高度)等等。 测试基于测得的心率和心率变异。心率和心率变异的改变会反映心脏自主调节的变化。 进行测试 对于恢复测试,您需要使用 Polar H9/H10 心率传感器(如果您有 H6 或 H7,也可以使用)。测试持

  • 我有一个Maven项目设置。在这个项目中,我使用JUnit进行单元测试。当我使用或运行测试时,不会运行任何测试。测试位于中,它们都以结束(它们匹配)。 这是我的pom.xml: 我的整个项目结构可以在这里看到:https://bitbucket.org/equator-lang/ppp4e/src/master/ 任何帮助都将非常感谢,因为我已经尝试了很多个小时来让这个工作,但不知道该做什么。 公