在Android意图中传递模拟类

时间:2018-05-17 15:20:34

标签: android mockito instrumentation

有没有办法在仪器测试期间将模拟类放入(android)意图中。以下几行有哪些内容?

var totalBytesUsed = myAccountInformation.Metrics.Sum(storageMetric => storageMetric.BytesUsed);
textBox1.Text = FormatBytes(myAccountInformation.TotalQuota) + "\r\n" + FormatBytes(myAccountInformation.UsedQuota) + "\r\n" + totalBytesUsed;

以上代码以@RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { @Mock(serializable = true) SomeClass someClass; //Defined also as implementing Serializable @Rule public ActivityTestRule<MainActivity> mMainActivityTestRule = new ActivityTestRule<MainActivity>(MainActivity.class,false,false); @Rule public MockitoRule rule = MockitoJUnit.rule(); @Test public void testGetQuote() throws Exception { Intent i = new Intent(); Bundle b = new Bundle(); b.putSerializable("someClass", someClass); i.putExtras(b); //Fails! // more code here }

失败

有什么想法吗?

---编辑 使用@Spy注释的代码如下所示:

Parcelable encountered IOException writing serializable object

...

@RunWith(AndroidJUnit4.class)
class ExampleInstrumentedTest {
@Spy
SomeClass someClass; //Same behaviour when @Mock was used
@Spy
Intent intent;
@Rule
public ActivityTestRule<MainActivity> mMainActivityTestRule = new
        ActivityTestRule<MainActivity>(MainActivity.class,false,false);
@Rule
public MockitoRule rule = MockitoJUnit.rule();

@Test
public void testGetQuote() throws Exception {
   when(someClass.getFoo(anyString())).thenReturn(expectedAnswer);
   when(intent.getSerializableExtra(anyString())).thenReturn(someClass);

   //Intent object has identifier of, e.g. @5650 here
   intent.putExtra("someClass",someClass);
   mMainActivityTestRule.launchActivity(intent);
   //more Code here
}

0 个答案:

没有答案
相关问题