3

@Mock(answer=Answers.RETURNS_SMART_NULL)予期しないモック呼び出しが発生したときに SmartNullPointerException を取得するために、Mockito 1.9.5 で注釈を使用しています。

残念ながら、少なくとも 1 つの重要な呼び出しをモックしなくても、テストはパスします。 明確にするために:私のポイントは、自分が欠けているものを自分で見つけることではなく、メソッドをモックしなかったためにテストに失敗することです使わずにやりたい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);
    }
}

コメント/アドバイス/説明ありがとうございます。

4

2 に答える 2