android atets 代码生成
副热带
·2,432 字符
·≈ 1,437 tokens
android atets 代码生成
提示词内容
帮忙生成这个文件的atest,要求:
- 帮忙生成这个文件的atest,要求:(AI prompt)
- (1) 测试类的非static private变量和方法使用反射获取和设置需要的参数,注意注入时候需要传入类的实例,例如 :field.set(instance, value);
- (2) 测试类的static private变量使用反射获取和设置需要的参数,注意注入时候无需要传入类的实例,例如:field.set(null, value);
- (3) 测试类的static 方法的获取,需要使用 ExtendedMockitoRule 的方法mockito;使用ExtendedMockitoRule 方法需要包含的包名:
import com.android.dx.mockito.inline.extended.ExtendedMockito;
import com.android.modules.utils.testing.ExtendedMockitoRule;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.*;
尽量使用ExtendedMockitoRule 的方式:例如:
@Rule
public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this)
.mockStatic(SystemProperties.class)
.mockStatic(ActivityManager.class)
.mockStatic(BatteryStatsService.class)
.build();
- (4) 单例类的优先使用注入instance 静态变量的方式进行mock,不要使用mockStatic的方式;
- (5) 如果有hander使用真实HandlerThread处理异步操作不要使用mock hander(符合要求);
- (6) 真机上运行,不要使用Robolectric框架,使用@RunWith(AndroidJUnit4.class)或者@RunWith(MockitoJUnitRunner.class)框架皆可;
- (7) 假如强制要使用@RunWith(AndroidJUnit4.class)的话,注意不能直接mock static 方法,否则会报错;
- (8) 验证死亡监听、重连逻辑、方法调用等核心功能,代码覆盖率 > 70%;
- (9) 每个test函数记得都要使用 throws Exception 捕获异常;
- (10) 每个test函数都再函数前附上解释的comment;
- (11) 对应测试类中的xxx. getService的调用,因为是静态函数方法,所以需要使用static mockito去模拟,然后使用下面的方式mock:(注意务必使用doReturn/doNothing等方式,不要使用when(() -> ...).thenReturn(...)的方式)
static 方法:
doReturn(mMockIPlatformCompat)
.when(() -> ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE));
doNothing().when(() -> SystemProperties.set(anyString(), anyString()));
非 static方法:
// 创建 mock SoundTriggerModule 并注入(通过反射)
mMockSTModule = mock(SoundTriggerModule.class);
doReturn(SoundTrigger.STATUS_OK).when(mMockSTModule).loadSoundModel(any(), any());
doReturn(SoundTrigger.STATUS_OK).when(mMockSTModule).unloadSoundModel(anyInt());
doReturn(SoundTrigger.STATUS_OK).when(mMockSTModule).startRecognition(anyInt(), any());
doReturn(SoundTrigger.STATUS_OK).when(mMockSTModule).stopRecognition(anyInt());
injectField(mAcdHelper, "mSTModule", mMockSTModule);
- (12) 对应测试类中的public的调用的mock,必须使用下面的方式:
对于public 函数的mock 记住要使用下面的写法,不能使用 doReturn
// doReturn(false).when(() -> mMockAtms.isInSplitScreenWindowingMode());
when(mMockAtms.isInSplitScreenWindowingMode()).thenReturn(false);
讨论