0
我寫在Android Studio中一個單元測試,並使用PowerMockPowerMock嘲笑MoreAsserts
測試看起來像這樣
@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嘲笑MoreAsserts沒有我問它...我錯過了什麼?
感謝, 斯拉瓦