I'm using the annotation @Mock(answer=Answers.RETURNS_SMART_NULL)
with Mockito 1.9.5 in order to get some SmartNullPointerException when some unexpected mock calls occurs.
Unfortunately, the test pass, even without mocking at least one important call.
To be clear : My point is not to find by myself what I'm missing but to fail the test because I didn't mock the methods. I would want to do it without using Mockito.verifyNoMoreInteractions(...)
My test :
@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
}
The code to be tested :
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);
}
}
Thanks for your comments / advices / explainations.
You need to call a method on the
null
objectsensor
for the test to fail.It seems like you are never using the
null
objectsensor
. This means you won't get anyNullPointerExceptions
(smart or not)you could do an
or just a
to get an exception. But
won't be sufficient to get the SmartNullPointerException