使用dom4j时候报错java.lang.verifyerror

博客提到将dom4j压缩包中的jaxen.jar添加到libs文件夹的操作。这涉及到开发过程中资源文件的配置,有助于相关项目的正常运行。
把dom4j的压缩包里面的jaxen.jar也加入到libs文件夹中即可。
package com.st.systemsettings.manager; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import android.content.Context; import android.util.Log; import java.lang.reflect.Field; import java.util.concurrent.CountDownLatch; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest({Log.class, OptixDisplayManager.class}) public class PictureManagerTest { @Mock private Context mockContext; @Mock private PictureManager.PictureModeChangedListener mockListener; @Mock private OptixDisplayManager mockOptixDisplayManager; private PictureManager pictureManager; @Before public void setUp() { try { // 初始化 Mockito 注解 MockitoAnnotations.openMocks(this); // 准备静态模拟 prepareStaticMocks(); // 重置单例状态 resetSingleton(); // 创建实例并初始化 pictureManager = PictureManager.getInstance(); pictureManager.init(mockContext); // 注入模拟的 OptixDisplayManager injectMockOptixDisplayManager(); // 设置模拟监听器 pictureManager.setPictureModeChangedListener(mockListener); } catch (Exception e) { throw new RuntimeException("Setup failed", e); } } @After public void tearDown() { try { resetSingleton(); } catch (Exception e) { // 安全地忽略清理错误 } } // 准备静态模拟 private void prepareStaticMocks() { // 模拟 Log 类 PowerMockito.mockStatic(Log.class); PowerMockito.when(Log.i(anyString(), anyString())).thenReturn(0); // 模拟 OptixDisplayManager 的单例 PowerMockito.mockStatic(OptixDisplayManager.class); PowerMockito.when(OptixDisplayManager.getInstance()).thenReturn(mockOptixDisplayManager); } // 修复后的重置单例方法 - 不再抛出受检异常 private void resetSingleton() { try { Field instanceField = PictureManager.class.getDeclaredField("sManager"); instanceField.setAccessible(true); instanceField.set(null, null); } catch (Exception e) { // 将受检异常转换为运行时异常 throw new RuntimeException("Failed to reset singleton", e); } } // 修复后的注入方法 - 不再抛出受检异常 private void injectMockOptixDisplayManager() { try { Field optixField = PictureManager.class.getDeclaredField("mOptixDisplayManager"); optixField.setAccessible(true); optixField.set(pictureManager, mockOptixDisplayManager); } catch (Exception e) { throw new RuntimeException("Failed to inject mock", e); } } @Test public void testSingletonPattern() { PictureManager firstInstance = PictureManager.getInstance(); PictureManager secondInstance = PictureManager.getInstance(); assertSame(firstInstance, secondInstance); } @Test public void testInitSetsContextCorrectly() { try { Field contextField = PictureManager.class.getDeclaredField("mContext"); contextField.setAccessible(true); Context privateContext = (Context) contextField.get(pictureManager); assertSame(mockContext, privateContext); } catch (Exception e) { fail("Reflection failed: " + e.getMessage()); } } @Test public void testSetPictureModeCallsOptixManager() { int testMode = 2; pictureManager.setPictureMode(testMode); verify(mockOptixDisplayManager).setCanvasSize(testMode); } @Test public void testSetPictureModeTriggersListener() { int testMode = 3; pictureManager.setPictureMode(testMode); verify(mockListener).onPictureModeChanged(testMode); } @Test public void testSetPictureModeWithoutListener() { pictureManager.setPictureModeChangedListener(null); int testMode = 4; pictureManager.setPictureMode(testMode); verify(mockListener, never()).onPictureModeChanged(anyInt()); } @Test public void testGetPictureModeReturnsCorrectValue() { int expectedMode = 5; when(mockOptixDisplayManager.getCanvasSize()).thenReturn(expectedMode); int result = pictureManager.getPictureMode(); assertEquals(expectedMode, result); } @Test public void testListenerIsSetCorrectly() { PictureManager.PictureModeChangedListener newListener = mock(PictureManager.PictureModeChangedListener.class); pictureManager.setPictureModeChangedListener(newListener); pictureManager.setPictureMode(1); verify(newListener).onPictureModeChanged(1); verify(mockListener, never()).onPictureModeChanged(anyInt()); } @Test public void testReInitUpdatesContext() { try { Context newContext = Mockito.mock(Context.class); pictureManager.init(newContext); Field contextField = PictureManager.class.getDeclaredField("mContext"); contextField.setAccessible(true); Context privateContext = (Context) contextField.get(pictureManager); assertSame(newContext, privateContext); } catch (Exception e) { fail("Reflection failed: " + e.getMessage()); } } @Test public void testThreadSafety() throws InterruptedException { resetSingleton(); // 现在安全了 CountDownLatch latch = new CountDownLatch(2); PictureManager[] instances = new PictureManager[2]; Thread t1 = new Thread(() -> { instances[0] = PictureManager.getInstance(); latch.countDown(); }); Thread t2 = new Thread(() -> { instances[1] = PictureManager.getInstance(); latch.countDown(); }); t1.start(); t2.start(); latch.await(); assertNotNull(instances[0]); assertSame(instances[0], instances[1]); } @Test public void testLoggingInSetPictureMode() { int testMode = 6; pictureManager.setPictureMode(testMode); PowerMockito.verifyStatic(Log.class); Log.i("PictureManager", "setPictureMode: mode = " + testMode); } @Test public void testLoggingInGetPictureMode() { int expectedMode = 7; when(mockOptixDisplayManager.getCanvasSize()).thenReturn(expectedMode); pictureManager.getPictureMode(); PowerMockito.verifyStatic(Log.class); Log.i("PictureManager", "getPictureMode: result = " + expectedMode); } @Test public void testNullContextHandling() { resetSingleton(); // 现在安全了 PictureManager uninitialized = PictureManager.getInstance(); uninitialized.setPictureMode(1); verify(mockOptixDisplayManager, never()).setCanvasSize(anyInt()); } @Test public void testMultipleInitializations() { Context secondContext = Mockito.mock(Context.class); pictureManager.init(secondContext); // 应该成功执行,不会抛出异常 pictureManager.setPictureMode(8); verify(mockOptixDisplayManager).setCanvasSize(8); } @Test public void testListenerRemoval() { pictureManager.setPictureModeChangedListener(null); pictureManager.setPictureMode(9); verify(mockListener, never()).onPictureModeChanged(anyInt()); } } 上面是我的测试代码,下面是出现的报错,请告诉我怎么解决 Failed to transform class with name com.st.systemsettings.manager.OptixDisplayManager. Reason: [source error] cannot find constructor android.os.UserHandle(int) java.lang.IllegalStateException: Failed to transform class with name com.st.systemsettings.manager.OptixDisplayManager. Reason: [source error] cannot find constructor android.os.UserHandle(int)
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值