问题简单描述
被测试类的构造函数中有特殊情况,需要在初始化的时候进行mock处理,否则会报错
具体情况
- 被测试类Java代码
public class FordCeDTEManager extends FordBaseManager {
private static final String TAG = "FordCeDTEManager";
private static FordCeDTEManager manager = null;
private CeDteMsgManager mSoaGatewayCommonManager;
private SoaGateway mSoaGateway;
private List<ISoaDTEListener> mISoaDTEListeners;
public FordCeDTEManager(Context context) {
super(context);
mISoaDTEListeners = new ArrayList<>();
//特殊的创建,需要mock处理
mSoaGateway = SoaGateway.createSoaGateway(mContext, mSoagatewayConnectionListener);
if (mSoaGateway != null) {
mSoaGateway.connect();
} else {
Log.d(TAG, "mSoaGateway is null");
}
}
public static synchronized FordCeDTEManager getInstance(Context context) {
if (null == manager) {
manager = new FordCeDTEManager(context);
}
return manager;
}
}
以及情况2
public FordBtManager(Context mContext) {
super(mContext);
this.mIMissCallsCountsChangeListeners = new ArrayList<>();
this.mIBluetoothEarphoneConnectStatusChangeListeners = new ArrayList<>();
this.mIBluetoothConnectStatusChangeListeners = new ArrayList<>();
//这句要注意
mSvDataBusManager = SvDataBusManager.get(mContext);
desayBluetoothSwitchManager = new DesayBluetoothSwitchManager(mSvDataBusManager);
desayMissCallManager = new DesayMissCallManager(mSvDataBusManager);
desayMissCallManager.registerMissCallsCountsListener(mSvDataBusDispatcher);
desayBluetoothSwitchManager.registerBluetoothEarphoneConnectStatusListener(mSvDataBusDispatcher1);
desayBluetoothSwitchManager.registerHFPStatusListener(mSvDataBusDispatcher2);
desayBluetoothSwitchManager.registerNotDisturbListener(dispatcher);
desayBluetoothSwitchManager.registerSilentAndPrivateListener(dispatcher);
}
- 问题具体分析
特殊创建要使用静态处理 - 具体测试代码
class FordCeDTEManagerUT {
private FordCeDTEManager target;
private AutoCloseable closeable;
private static MockedStatic<SoaGateway> staticSoaGateway;
private static MockedStatic<Log> staticLog;
private MockedConstruction<Object> constructionObject;
private desaysv.soagateway.SoaCedteTripInfo mocksoaCedteTripInfo;
//全局mock
@Mock
private SoaGateway soaGateway;
@BeforeAll
public static void beforeAll() {
//静态处理,具体逻辑需要补充
staticSoaGateway = Mockito.mockStatic(SoaGateway.class);
staticLog = Mockito.mockStatic(Log.class);
}
@AfterAll
public static void afterAll() {
staticSoaGateway.close();
staticLog.close();
}
@BeforeEach
public void beforeEach() {
closeable = MockitoAnnotations.openMocks(this);
mocksoaCedteTripInfo = Mockito.mock(desaysv.soagateway.SoaCedteTripInfo.class);
target = FordCeDTEManager.getInstance(Mockito.mock(Context.class));
}
@AfterEach
public void afterEach() throws NoSuchFieldException, IllegalAccessException {
try {
closeable.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
//单例模式处理
Class c = target.getClass();
Field manager = c.getDeclaredField("manager");
manager.setAccessible(true);
manager.set(target, null);
}
}
以及情况2
class FordBtManagerUT extends BaseUnitTest {
private FordBtManager fordBtManager;
private static MockedStatic<SvDataBusManager> staticSvDataBusManager;
private static MockedStatic<BtCommonManager> staticBtCommonManager;
private static MockedStatic<BtPbapManager> staticBtPbapManager;
private static MockedStatic<BtHfpManager> staticBtHfpManager;
private static MockedStatic<BtA2dpManager> staticBtA2dpManager;
private static MockedStatic<Log> logMockedStatic;
@Mock
private BtCommonManager btCommonManager;
@Mock
private BtPbapManager btPbapManager;
@Mock
private BtHfpManager btHfpManager;
@Mock
private BtA2dpManager btA2dpManager;
@Mock
private Context context;
@BeforeAll
public static void beforeAll() {
staticSvDataBusManager = Mockito.mockStatic(SvDataBusManager.class);
logMockedStatic = Mockito.mockStatic(Log.class);
staticBtA2dpManager = Mockito.mockStatic(BtA2dpManager.class);
staticBtCommonManager = Mockito.mockStatic(BtCommonManager.class);
staticBtPbapManager = Mockito.mockStatic(BtPbapManager.class);
staticBtHfpManager = Mockito.mockStatic(BtHfpManager.class);
}
@BeforeEach
public void beforeEach() {
context = Mockito.mock(Context.class);
SvDataBusManager svDataBusManager = Mockito.mock(SvDataBusManager.class);
when(context.getApplicationContext()).thenReturn(context);
when(context.getContentResolver()).thenReturn(Mockito.mock(ContentResolver.class));
Mockito.when(SvDataBusManager.get(context)).thenReturn(svDataBusManager);
fordBtManager = FordBtManager.getInstance(context);
}
@AfterEach
public void afterEach() throws NoSuchFieldException, IllegalAccessException {
Class c = fordBtManager.getClass();
Field manager = c.getDeclaredField("manager");
manager.setAccessible(true);
manager.set(fordBtManager, null);
}
@AfterAll
public static void afterAll() {
staticBtA2dpManager.close();
logMockedStatic.close();
staticBtCommonManager.close();
staticBtPbapManager.close();
staticBtHfpManager.close();
staticSvDataBusManager.close();
}
存在问题
技术细节
1.静态
小结
- 没怎么理解逻辑,是学习别人的代码