Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用Mockito模拟lombok@Getter字段_Java_Unit Testing_Mocking_Mockito_Lombok - Fatal编程技术网

Java 用Mockito模拟lombok@Getter字段

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

我在一些静态字段上使用了带Lombok的
@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 ?