我最近开始阅读有关 Mockito 的内容。根据我的理解,以下代码行必须返回 true,但它返回 false。
测试班
public class PersonServiceImplTest {
Car car;
@InjectMocks
CarServiceImpl carService;
@Mock
CarDAOImpl carDAO;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testUpdateCar() {
int carId = 1;
Mockito.when(carDAO.getCarById(any(Integer.class))).thenReturn(new Car());
carService.updateCar(carId);
Mockito.when(carDAO.isLicenseExpired(any(Car.class))).thenReturn(true);
Mockito.verify(carDAO).updateCar(any(Car.class));
Mockito.verify(carDAO, times(1)).isLicenseExpired(any(Car.class));
Mockito.verify(carDAO, times(1)).issueLicense(any(Car.class));
}
}
Run Code Online (Sandbox Code Playgroud)
待测试类
public class CarServiceImpl implements CarService {
@Autowired carDAO carDAO;
@Override
public Response updateCar(int carId) {
Car car =carDAO.getCarById(carId);
try {
carDAO.updateCar(car);
if(carDAO.isLicenseExpired(car)))
carDAO.issueLicense(car);
} catch (Exception e) {
log.error(e.getMessage());
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
return Response.ok(Status.CREATED).build(); …Run Code Online (Sandbox Code Playgroud)