在模拟java.lang.System时出现此错误
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.lang.System
Mockito cannot mock/spy because :
- final class
我的代码是什么样的:
@RunWith(PowerMockRunner.class)
@PrepareForTest(System.class)
public class UaUtilsTest {
@Mock
Context context;
@Mock
PackageManager packageManager;
@Mock
PackageInfo packageInfo;
@Before
public void setUp() throws Exception {
PowerMockito.mockStatic(System.class);
when(System.getProperty("http.agent")).thenReturn("user agent");
}
我已经尝试过: 1.使用PowerMockito模拟静态 2.存根getProperty方法调用
此错误有解决方案吗?