简体   繁体   English

单元测试-测试@Retryable和@Recover的问题

[英]Unit test - Problems testing @Retryable and @Recover

I have a component that's using @Retryable annotation and another service using that component. 我有一个使用@Retryable批注的组件,另一个使用该组件的服务。 So I'm trying to test that the component using @Retryable annotation is actually retrying. 因此,我试图测试使用@Retryable批注的组件是否正在重试。

I've tried every solution there is on the web right now but nothing worked for me. 我已经尝试了网络上目前存在的所有解决方案,但对我没有任何帮助。 I'm trying to create a unit test for this and not integration test. 我正在尝试为此创建单元测试,而不是集成测试。 So far I've managed to get to the exception that's supposed to be thrown and @Retryable wasn't even retrying, the method just threw the exception and thats it. 到目前为止,我已经设法解决了应该抛出的异常,并且@Retryable甚至没有重试,该方法只是抛出了异常,仅此而已。

This is the component using Retryable annotation: 这是使用Retryable批注的组件:

@Component
public class OurComponent {

    @Retryable(maxAttempts = 10,
            backoff = @Backoff(delay = 2000),
            value = {someException.class}
    )
    public void someMethod(SomeObject someObject) throws someException {
        Object createObject = anotherMethod(someObject); //this method throws someException
        ...
    }
}

And the service using this ourComponent: 以及使用此ourComponent的服务:

@Service
public class someService {

    private final OurComponent ourComponent;

    public SomeService(OurComponent ourComponent) {
         this.ourComponent = ourComponent;
    }

    ...


    public void methodUsingComponent() {
         SomeObject someObject = new SomeObject(args);
         ourComponent.someMethod(someObject);
    }
}

Now I've tried to @InjectMocks and @MockBean this service and component but it still didn't work. 现在,我尝试使用@InjectMocks和@MockBean这个服务和组件,但是仍然无法正常工作。 Is it even possible to test @Retryable annotation without doing integration test? 甚至不进行集成测试就可以测试@Retryable注释吗?

If you use a unit test that doesn't use spring at all, you won't be able to test it easily. 如果您使用根本不使用spring的单元测试,则将无法轻松对其进行测试。

This is due to the fact that annotations like this are recognized by spring and the corresponding bean is wrapped with a runtime-generated proxy that implements the "retry" logic. 这是由于这样的事实,Spring会识别出这样的注释,并且相应的bean被运行时生成的代理包装,该代理实现了“重试”逻辑。

Now, if you don't have spring who triggers all this mechanism, this @Retryable annotation is basically useless, mockito doesn't know anything about, so is Junit. 现在,如果您没有触发所有此机制的spring,则此@Retryable注释基本上是无用的,mockito对此一无所知,Junit也不知道。

You could try to create a proxy like this manually (check what logic spring-retry invokes) but it looks to be an overkill. 您可以尝试手动创建一个这样的代理(检查spring-retry调用的逻辑),但这看起来有些过头了。 And frankly speaking, it doesn't give you anything. 坦白说,它什么也没给你。 Unit test should check the functionality of your code and not the logic behind spring retry that was implemented by somewhere else and tested. 单元测试应该检查代码的功能,而不是检查弹簧重试背后由其他地方实现并经过测试的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM