[英]Mocking lombok @Getter fields with Mockito
I am using @Getter
notation with Lombok on some of my static fields as such: 我在一些静态字段@Getter
表示法与Lombok一起使用:
public class A {
@Getter protected static MyClass myClass;
}
While unit testing, I have to mock the value of these static fields for a section of code that does: 在进行单元测试时,我必须为一部分代码模拟这些静态字段的值:
MyClass.getMyClass();
To mock, I'm doing: 为了模拟,我在做:
mock(MyClass.class);
when(MyClass.getMyClass()).thenReturn(...);
However, such mock gives below error. 但是,这样的模拟给出了以下错误。
[testng] org.mockito.exceptions.misusing.MissingMethodInvocationException:
[testng] when() requires an argument which has to be 'a method call on a mock'.
[testng] For example:
[testng] when(mock.getArticles()).thenReturn(articles);
[testng]
[testng] Also, this error might show up because:
[testng] 1. you stub either of: final/private/equals()/hashCode() methods.
[testng] Those methods *cannot* be stubbed/verified.
[testng] Mocking methods declared on non-public parent classes is not supported.
[testng] 2. inside when() you don't call method on mock but on some other object.
I must be hitting condition 2, but I'm not understanding how I am not "calling method on mock". 我必须达到条件2,但我不了解自己是不是“在模拟中调用方法”。
Has anyone successfully mocked Lombok getters? 有谁成功嘲笑龙目岛吸气剂?
Thanks! 谢谢!
As I said in comment above, Mockito do not support mocking static methods. 正如我在上面的评论中所述,Mockito不支持模拟静态方法。
Example: 例:
@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class YourTestClass{
PowerMockito.mockStatic(A.class);
when(A.getMyClass()()).thenReturn(...);
}
Also, 也,
MyClass.getMyClass();
getMyClass() belongs to class A or class Myclass ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.