gpt4 book ai didi

android - PowerMock 模拟 MoreAsserts

转载 作者:行者123 更新时间:2023-11-30 01:30:34 25 4
gpt4 key购买 nike

我正在 android studio 中编写单元测试并使用 PowerMock

测试看起来像这样

@RunWith(PowerMockRunner.class)
@PrepareOnlyThisForTest({Typeface.class})
public class ExtendedTextViewTest {

...

@Test
public void ctor_context_attributeSet_ShouldSetTypeface() throws Exception {
// Act
final LinkedList<Typeface> actualTypefaces = new LinkedList<Typeface>();
ExtendedTextView textView = spy(new ExtendedTextView(contextMock, attributeSet) {
@Override
public void setTypeface(Typeface typeface) {
actualTypefaces.add(typeface);
}
});

// Assert
MoreAsserts.assertEquals(new Typeface[]{typefaceMock}, actualTypefaces.toArray());
verify(typedArrayMock, times(1)).recycle();
}

}

当我运行测试时出现错误:

java.lang.RuntimeException: Method assertEquals in android.test.MoreAsserts not mocked. See http://g.co/androidstudio/not-mocked for details.
at android.test.MoreAsserts.assertEquals(MoreAsserts.java)
at com.loka.loka.common.ExtendedTextViewTest.ctor_context_attributeSet_ShouldSetTypeface(ExtendedTextViewTest.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)

似乎 mockito 在没有我要求的情况下 mock 了 MoreAsserts ...我错过了什么?

谢谢,光荣

最佳答案

好的,搜索后我找到了答案......MoreAsserts是一个android sdk类(android.test.MoreAsserts)

当运行单元测试时,android sdk 没有加载,而是所有的类都被一个抛出异常的实现 stub ,包括 MoreAsserts..

所以我需要用 JUNit 断言替换断言:Assert.assertArrayEquals

Assert.assertArrayEquals(new Typeface[]{typefaceMock}, actualTypefaces.toArray());

希望对大家有所帮助=]

关于android - PowerMock 模拟 MoreAsserts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35800075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com