在我的 Java 代码中,我想用 Mockito 模拟一个返回布尔值的方法。事实上,它涉及一个计算停车价格的停车应用程序。在我使用 Junit5 和 Mockito 进行的测试中,我模拟了一个方法,该方法返回一个布尔值,如果该布尔值为真,则通过减少来计算价格。但我的模拟不起作用,我不知道为什么......
when(ticketDao.isRegularCustomer(any(String.class))).thenReturn(true);
Run Code Online (Sandbox Code Playgroud)
我希望这个模拟可以通过减少来计算价格。感谢您的帮助,这是我的代码。
@ExtendWith(MockitoExtension.class)
public class FareCalculatorServiceTest {
private static FareCalculatorService fareCalculatorService;
private Ticket ticket;
@Mock
private static TicketDao ticketDao;
@BeforeAll
private static void setUp() {
fareCalculatorService = new FareCalculatorService();
}
@BeforeEach
private void setUpPerTest() {
ticket = new Ticket();
//when(ticketDao.isRegularCustomer(any())).thenReturn(false);
}
@Test
public void feature2CalculateFareBikeWith5PerCentReduceIfRegularCustomer(){
Date inTime = new Date();
inTime.setTime( System.currentTimeMillis() - (45 * 60 * 1000) );//45MIN IN PARKING
Date outTime = new Date();
ParkingSpot parkingSpot = new ParkingSpot(1, …Run Code Online (Sandbox Code Playgroud)