小编Sah*_*hai的帖子

Mockito-thenReturn(true) 仍然在模拟对象上返回 false

我最近开始阅读有关 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)

testing unit-testing mockito

3
推荐指数
1
解决办法
9179
查看次数

标签 统计

mockito ×1

testing ×1

unit-testing ×1