Mock&Stub System.getProperty,Mockito无法模拟/间谍,因为'final'类

时间:2020-03-18 09:51:31

标签: android unit-testing mockito powermockito

在模拟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方法调用

此错误有解决方案吗?

0 个答案:

没有答案