Java 用Mockito模拟lombok@Getter字段
我在一些静态字段上使用了带Lombok的Java 用Mockito模拟lombok@Getter字段,java,unit-testing,mocking,mockito,lombok,Java,Unit Testing,Mocking,Mockito,Lombok,我在一些静态字段上使用了带Lombok的@Getter符号,例如: public class A { @Getter protected static MyClass myClass; } 在进行单元测试时,我必须模拟一段代码的这些静态字段的值: MyClass.getMyClass(); 为了嘲弄,我在做: mock(MyClass.class); when(MyClass.getMyClass()).thenReturn(...); 然而,这种模拟给出了以下错误 [test
@Getter
符号,例如:
public class A {
@Getter protected static MyClass myClass;
}
在进行单元测试时,我必须模拟一段代码的这些静态字段的值:
MyClass.getMyClass();
为了嘲弄,我在做:
mock(MyClass.class);
when(MyClass.getMyClass()).thenReturn(...);
然而,这种模拟给出了以下错误
[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.
我必须达到条件2,但我不明白我为什么不是“在mock上调用方法”
有人成功地嘲弄了龙目达人吗
谢谢 正如我在上面的评论中所说,Mockito不支持模拟静态方法 使用 例如:
@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class YourTestClass{
PowerMockito.mockStatic(A.class);
when(A.getMyClass()()).thenReturn(...);
}
而且
正如我在上面的评论中所说,Mockito不支持模拟静态方法 使用 例如:
@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class YourTestClass{
PowerMockito.mockStatic(A.class);
when(A.getMyClass()()).thenReturn(...);
}
而且
getMyClass()必须是静态的吗?mockito支持静态方法模拟吗?getMyClass()必须是静态的?mockito支持静态方法模拟吗?
getMyClass() belongs to class A or class Myclass ?