我将注释@Mock(answer=Answers.RETURNS_SMART_NULL)
与 Mockito 1.9.5 一起使用,以便在发生一些意外的模拟调用时获得一些 SmartNullPointerException。
不幸的是,测试通过了,即使没有嘲笑至少一个重要的电话。
明确一点:我的意思不是自己找到我缺少的东西,而是因为我没有模拟这些方法而未能通过测试。我想在不使用的情况下做到这一点Mockito.verifyNoMoreInteractions(...)
我的测试:
@RunWith(MockitoJUnitRunner.class)
public class ContextServiceImplTest {
@Mock(answer = Answers.RETURNS_SMART_NULLS)
private IAccountDAO accountDaoMock;
@Mock(answer = Answers.RETURNS_SMART_NULLS)
private IRuleService ruleServiceMock;
@Mock(answer = Answers.RETURNS_SMART_NULLS)
private ISensorDAO sensorDAOMock;
private ContextServiceImpl contextService;
@Before
public void before() {
contextService = new ContextServiceImpl(accountDaoMock, ruleServiceMock, sensorDAOMock);
}
@Test
public void fillSensor() throws Exception {
// given
String givenSensorId = "123"
final EventDTO givenEvent = new EventDTO();
givenEvent.setSensorId(givenSensorId)
// when
final Context context = contextService.getContext(givenEvent);
// then (should fail and throw explicit exception
}
要测试的代码:
public class ContextServiceImpl {
...
public Context getContext(final EventDTO event) throws Exception {
final String sMethodName = "getContext";
final String sensorId = event.getSensorId();
Context context = new Context();
try {
final Sensor sensor = sensorDAO.findById(sensorId);
context.setSensor(sensor);
return context;
} catch (final NoResultException nre) {
throw new BusinessException(ValidationCode.UNSUPPORTED_VALUE, "sensorId");
} catch (final PersistenceException pe) {
throw new TechnicalException(TechnicalCode.DATABASE_ACCESS_PROBLEM);
}
}
感谢您的意见/建议/解释。