我有些代码如下所示。
@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)
}RETRY_ATTEMPTS和RETRY_DELAY变量的值来自一个独立的常量类,它们是int基元。这两个变量都被定义为公共静态终结。
如何在编写单元测试用例时重写这些值。实际值会增加单元测试用例的运行时间。
我已经尝试过两种方法:两种方法都没有用。
编辑:
正如@yegor256所提到的,这是不可能的,我想知道,为什么不可能呢?当这些注释加载的时候?
发布于 2015-09-26 21:19:50
无法在运行时更改它们。为了使您的method()可测试,您应该做的是创建一个单独的“装饰器”类:
interface Foo {
void method();
}
class FooWithRetry implements Foo {
private final Foo origin;
@Override
@RetryOnFailure(attempts = Constant.RETRY_ATTEMPTS)
public void method() {
this.origin.method();
}
}然后,为了测试目的,使用Foo的另一个实现
class FooWithUnlimitedRetry implements Foo {
private final Foo origin;
@Override
@RetryOnFailure(attempts = 10000)
public void method() {
this.origin.method();
}
}这是你能做的最好的了。不幸的是。
https://stackoverflow.com/questions/31650255
复制相似问题