- verifyNoMoreInteractions(listener);//验证没有更多交互
- @Mock private PlaybackParametersListener listener; initMocks(this);在使用 Androidunit4 时同样可以使用 Mockito;
- assertThat(mediaClock.syncAndGetPositionUs( false)).isEqualTo(TEST_POSITION_US);
- @Config(sdk = 29) // Allows using MediaFormat.getKeys() to make assertions over the expected keys.//写在类名上方
- 单元测试:上下文:context = InstrumentationRegistry.getTargetContext();
- Context applicationContext = ApplicationProvider.getApplicationContext()
- 反射调用私有静态方法
Method method = Camera1Enumerator.class.getDeclaredMethod("getCameraInfo", Integer.class);
method.setAccessible(true);
android.hardware.Camera.CameraInfo cameraInfo = (android.hardware.Camera.CameraInfo) method.invoke(mCamera1Enumerator, 0);
Assert.assertNotNull( cameraInfo);
-
屏蔽静态代码块加载:@SuppressStaticInitializationFor(“com.rz.paas.internal.RZRtcEngineImpl”)//阻止静态代码块运行 -
反射回去静态属性‘ private Class reflectClass, reflectRecord; //用以获取私有属性 private Field mbluetoothManager, maudioManager; reflectClass = mDBAudioManager.getClass(); reflectRecord = mDBAudioRecord.getClass(); mbluetoothManager = reflectClass.getDeclaredField(“bluetoothManager”);//私有属性 mbluetoothManager.setAccessible(true); //获取其值 mbluetoothManager.get(mDBAudioManager) //设置其值 mbluetoothManager.set(mDBAudioManager,value) -
描述
- 测试异常
@Test(expected = IllegalStateException.class) - 抽象类的反射
私有属性:
私有属性的变量 = 抽象类 .class.getDeclaredField("私有属性");
私有属性的变量.setAccessible(true);
方法
AbstractClass.class.getMethod("getName").invoke(子类对象);
- cameraName
cameraName = "Camera 0, Facing back, Orientation 90";
cameraName_Back = "Camera 1, Facing front, Orientation 270";
- 单元测试测试线程
Thread thread = new Thread(){
@Override
public void run() {
super.run();
for (int i = 0; i < 5; i++) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
@Test
public void createTextureBufferWithModifiedTransformMatrix() throws Exception{
thread.start();
thread.join();
System.out.println("线程执行结束");
}
thread.join(); 等待线程执行结束!
- 测试回调
interface Callback {
void onStart();
void onProgress(int progress);
void onFinish();
}
static class AsyncDemo {
private int len = 0;
private Callback callback = null;
public AsyncDemo(int len, Callback cb) {
this.len = len;
this.callback = cb;
}
public void doJob() {
new Thread() {
@Override
public void run() {
if (null != callback) {
callback.onStart();
}
try {
for (int i = 0; i < len; i++) {
if (null != callback) {
callback.onProgress(i);
}
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (null != callback) {
callback.onFinish();
}
}
}
}.start();
}
}
private Object mLock = new Object();
private int mCount = 0;
@Test
public void testBlindCallback() {
System.out.println("testBlindCallback");
Callback cb = new Callback() {
public void onStart() {
System.out.println("onStart");
}
public void onProgress(int progress) {
System.out.println("onProgress: " + progress);
}
public void onFinish() {
System.out.println("onFinish");
synchronized (mLock) {
mCount = 0;
mLock.notifyAll();
}
}
};
System.out.println("before AsyncDemo doJob");
AsyncDemo demo = new AsyncDemo(5, cb);
demo.doJob();
synchronized (mLock) {
mCount = 1;
while (mCount > 0) {
try {
mLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("after AsyncDemo doJob");
}
输出结果:
testBlindCallback
before AsyncDemo doJob
onStart
onProgress: 0
onProgress: 1
onProgress: 2
onProgress: 3
onProgress: 4
onFinish
after AsyncDemo doJob
- 初始化无参构造器
mCamera1Session = Camera1Session.class .newInstance(); - Mac 运行 .sh 文件,
sh ./build_libs.sh - 一个Volatile;
@Test
public void test() throws Exception {
AoFlag aoFlag = new AoFlag();
aoFlag.start();
for (int i = 0; i < 300; i++) {
if (aoFlag.isFlag())
Log.e("TAG\t\t\t", "test: ~~~ "+i);
}
aoFlag.join();
}
class AoFlag extends Thread {
boolean flag = false;
public boolean isFlag() {
return flag;
}
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = true;
Log.e("TAG\t\t\t", "run: " + flag);
}
}
- 乐观锁、悲观锁;
- Synchronized
- 理解协程、LiveData 和 Flow
- 没有LiveData如何实现轻松MVVM
声明
上述内容从网上搜集整理,如有侵权请私信我速删。
|