[英]Mocked class recognized as 'static member' is being called but it is not being recognized in the test as the mock?
我嘲笑了课堂,直到给我这个错误。 不知道我在这里做错了什么,但是我得到了错误: when() requires an argument which has to be 'a method call on a mock'
是包含以下内容的行when() requires an argument which has to be 'a method call on a mock'
Mockito.when(trdi.strToInteger(newValueIn)).thenReturn(0);
该类是模拟的,但我不明白为什么它会给我这个错误。 我的IDE也告诉我trdi.strToInteger是通过实例引用访问的静态成员。
public class TestApproved extends TestGroovy {
@Mock
private DataLayer dl;
@Mock
private DataStore dsIn;
@Mock
private DataStore ds;
@Mock
private TRDIUtils trdi;
@Parameterized.Parameter
public String client;
@Parameterized.Parameters(name = "{index}: {0}")
public static Object[] data() {
return new Object[]{
1, 2, 3, 4
};
}
private String newValueIn;
@Before
public void setUp() throws Exception {
//groovy script file to test
groovyScriptFile = new File(GROOVY_SCRIPTS_FOLDER + "/" + client + "/ToTest.groovy");
MockitoAnnotations.initMocks(this);
newValueIn = "1";
//groovy script parameters
addGroovyBindingVariable(GroovyScriptArgName.DATASTORE, dsIn);
addGroovyBindingVariable(GroovyScriptArgName.NEW_VALUE, newValueIn);
}
/**
* Test that user and date are nullified/disallowed to be updated if 'Approved' column is unchecked
*
*/
@Test
public void testUncheckedApproved() throws Exception{
Mockito.when(trdi.strToInteger(newValueIn)).thenReturn(0);
evaluate();
Mockito.verify(dsIn, Mockito.times(1)).setItemNull(1,"XX_APPROV_DATE");
Mockito.verify(dsIn, Mockito.times(1)).setItemNull(1,"XX_APPROV_USER");
}
为了模拟静态类,我们需要使用PowerMockito。
使用以下调用为您准备静态类方法以进行模拟
PowerMockito.mockStatic(TRDIUtils.class)
然后开始使用以下方法
PowerMockito.when(TRDIUtilsstrToInteger(newValueIn)).thenReturn(0);
希望这有助于根据您自己的逻辑对其进行自定义。 快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.