package com.atlassian.bitbucket.mockito;

import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;

/**
 * Simulates the work of running a unit test with Mockito's {@code MockitoJUnitRunner}, initialising any fields
 * which are annotated with {@code Mock}.
 */
public class MockitoTestExecutionListener extends AbstractTestExecutionListener {

    @Override
    public void prepareTestInstance(TestContext testContext) throws Exception {
        MockitoAnnotations.initMocks(testContext.getTestInstance());
    }

    @Override
    public void afterTestMethod(TestContext testContext) throws Exception {
        Mockito.validateMockitoUsage();
    }
}
