【问题标题】:Mockito- thenReturn(true) still returns false on a mock objectMockito-thenReturn(true) 仍然在模拟对象上返回 false
【发布时间】:2017-11-17 08:31:31
【问题描述】:

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

要测试的类

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();
}

CarDAOImpl 处理数据库,如果需要,也会更新它。

提前致谢。

【问题讨论】:

  • 感谢您的快速回复。我应该在我目前做 MockitoAnnotations.initMocks(this); 的 setUp 方法中做吗?还是在测试方法本身?
  • 取决于您是否要在多个测试中重复使用同一个对象 - 然后在设置中,否则,在测试本身中。您在代码中执行的位置:Mockito.when...thenReturn.. ?
  • 试过了,没用。
  • 因此,要么 (a) ServiceImpl 中的 SomeDaoImpl 实例在运行时 不是 模拟,要么 (b) mockedObject.anyMethod(any(Integer.class)) 不匹配。在您的测试调用期间,您是否验证ServiceImpl 中的SomeDaoImpl 实例是否为模拟?你能把anyMethod的定义贴在SomeDaoImpl上吗?
  • @glytching 我如何检查 (a) ?并且 (b) 是匹配的,因为我能够验证是否调用了 anyMethod。另外,当我 System.out.println(Mockito.when(mockedObject.anyMethod(any(Integer.class))).thenReturn(true)); 它确实打印错误。

标签: unit-testing testing mockito


【解决方案1】:

这两行排序错误:

carService.updateCar(carId); 
Mockito.when(carDAO.isLicenseExpired(Mockito.any(Car.class))).thenReturn(true);

该对中的第一行调用被测类,第二行设置您的carDAO 在被测类中应如何表现的期望。因此,您在调用被测类之后设置期望。

以下测试将通过:

@Test
public void testUpdateCar() {
    int carId = 1;

    // establish expectations of how carDAO should behave inside updateCar()
    Mockito.when(carDAO.getCarById(Mockito.any(Integer.class))).thenReturn(new Car());
    Mockito.when(carDAO.isLicenseExpired(Mockito.any(Car.class))).thenReturn(true);

    // invoke the class-under-test
    carService.updateCar(carId);

    // verify that CarService used CarDAO correctly
    Mockito.verify(carDAO).updateCar(Mockito.any(Car.class));
    Mockito.verify(carDAO).isLicenseExpired(Mockito.any(Car.class));
    Mockito.verify(carDAO).issueLicense(Mockito.any(Car.class));
}

【讨论】:

  • 我正在写一些其他方法并且它有效,所以我认为这是顺序。重新排序,它工作。回到这里寻找相同的答案。 :) 感谢您的时间和解释。
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2022-12-18
  • 2010-09-14
  • 2017-05-17
相关资源
最近更新 更多