我正在嘗試創建一個when句子,但我做得不好,因為我不知道如何正確地模擬任何東西。
我有這個代碼:
public class OperationMovement {
@Value("${operation.check}")
private Boolean needToCheck;
private void checkOperation() {
// Code
if (BooleanUtils.isTrue(needToCheck)) {
// More code
}
}
}
我需要用 Mockito 創建一個測驗,但我不知道如何模擬這個if else。
我試圖以這種方式模擬BooleanUtils:
@Mock
BeanUtils beanUtils;
// Code and more testing code
when(booleanUtils.isTrue(anyBoolean())).thenReturn(true);
但這會回傳一個錯誤。
我也嘗試了以下方法,但我有同樣的錯誤:
when(BooleanUtils.isTrue(anyBoolean())).thenReturn(true);
我需要模擬該屬性或 BooleanUtils 類,但我不知道如何。
uj5u.com熱心網友回復:
快速示例用法:
private OperationMovement classUnderTest;
...
@Test
void testOperationIsTrue() {
// For this test case, you are setting it to true
ReflectionTestUtils.setField(classUnderTest,"needToCheck",true);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504747.html
