Java 如何模拟jcabi注释参数
我有一些代码如下Java 如何模拟jcabi注释参数,java,unit-testing,junit,mocking,jcabi,Java,Unit Testing,Junit,Mocking,Jcabi,我有一些代码如下 @RetryOnFailure(attempts = Constant.RETRY_ATTEMPTS, delay = Constant.RETRY_DELAY, unit = TimeUnit.SECONDS) public void method() { // some processing //throw exception if HTTP operation is not successful. (use of retry) } 重试次数和重试延迟变量
@RetryOnFailure(attempts = Constant.RETRY_ATTEMPTS, delay = Constant.RETRY_DELAY, unit = TimeUnit.SECONDS)
public void method() {
// some processing
//throw exception if HTTP operation is not successful. (use of retry)
}
重试次数和重试延迟变量的值来自一个单独的常量类,它们是int原语。这两个变量都定义为公共静态final
如何在编写单元测试用例时重写这些值。实际值增加了单元测试用例的运行时间
我已经尝试过两种方法:两种都不起作用
编辑:
正如@yegor256所提到的,这是不可能的,我想知道,为什么这是不可能的?加载这些注释时?无法在运行时更改它们。为了使您的
方法()可测试,您应该做的是创建一个单独的“decorator”类:
然后,出于测试目的,使用Foo
的另一个实现:
class FooWithUnlimitedRetry implements Foo {
private final Foo origin;
@Override
@RetryOnFailure(attempts = 10000)
public void method() {
this.origin.method();
}
}
这是你能做的最好的了。不幸的是。目前,我实现了自己的类来进行重试,而不是使用注释。我肯定会尝试这种方法。我还有一个问题,为什么不可能覆盖这些注释?是的。我以前就明白了。我的意思是,注释有什么不同之处,即使通过模拟框架和java反射,它们也无法被覆盖
class FooWithUnlimitedRetry implements Foo {
private final Foo origin;
@Override
@RetryOnFailure(attempts = 10000)
public void method() {
this.origin.method();
}
}