前置步骤
- 所有单测的上一级目录下 新建 BaseTestCase
public class BaseTestCase { @Before public void init_mocks() { MockitoAnnotations.initMocks(this); } }
- 具体的单测类
@RunWith(PowerMockRunner.class) @WebAppConfiguration @Log4j2 public class BaseDataApiServiceImplTest extends BaseTestCase{ @InjectMocks private static InnerDataApiServiceImpl client; @Mock private static InterfaceCallLogService interfaceCallLogService; @Before public void setUp() throws Exception { client = new InnerDataApiServiceImpl(); ReflectionTestUtils.setField(client, "interfaceCallLogService", interfaceCallLogService); } @SneakyThrows @Test public void getApiDataByInterfaceName() { String interfaceName = CommonConstants.TEST_INTERFACENAME; String inputParams = CommonConstants.TEST_INPUTPARAMS; String accessToken = CommonConstants.TEST_ACCESSTOKEN; client.getApiDataByInterfaceName(interfaceName, inputParams, accessToken); } }