Android ActivityInstrumentationTestCase2中的模拟帐户

Android ActivityInstrumentationTestCase2中的模拟帐户,android,performance,unit-testing,junit,android-account,Android,Performance,Unit Testing,Junit,Android Account,在我的活动中,我在onCreate()中获取帐户: 现在,我正在测试项目中进行单元测试MyActivity: public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> { ... @Override protected void setUp() throws Exception{ super.setUp(); //How to mo

在我的活动中,我在
onCreate()
中获取帐户:

现在,我正在测试项目中进行单元测试
MyActivity

public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
    ...
    @Override
    protected void setUp() throws Exception{
       super.setUp();
      //How to mock up the accounts in system so that some fake accounts could be used
    }
    ...
}
公共类MyActivityTest扩展了ActivityInstrumentationTestCase2{
...
@凌驾
受保护的void setUp()引发异常{
super.setUp();
//如何在系统中模拟帐户以便使用一些假帐户
}
...
}
在我上面的测试用例中,我想使用一些假帐户,我如何模拟帐户,以便
AccountManager.get(this.getAccounts()返回测试项目中的模拟帐户?

尝试以下代码:

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(AccountManager.class)
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
{
    @Mock
    public MyActivity myActivity;

    @Mock
    AccountManager accountManager;

    @Before
    public void setUp() throws Exception{
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void mocking() {
        mockStatic(AccountManager.class);
        when(AccountManager.get(any(MyActivity.class))).thenReturn(accountManager);
        when(accountManager.getAccounts()).thenReturn(new Account[] {});
        MyActivity activity = new MyActivity();
        activity.onCreate();
        assertEquals(0, activity.getAccounts().length);
    }

    @Test
    public void withoutMocking() {
        MyActivity activity = new MyActivity();
        activity.onCreate();
        assertEquals(2, activity.getAccounts().length);
    }

}
导入静态org.junit.Assert.assertEquals;
导入静态org.mockito.Matchers.any;
导入静态org.powermock.api.mockito.PowerMockito.mockStatic;
导入静态org.powermock.api.mockito.PowerMockito.when;
导入org.junit.Before;
导入org.junit.BeforeClass;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.mockito.Mock;
导入org.mockito.MockitoAnnotations;
导入org.powermock.core.classloader.annotations.PrepareForTest;
导入org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(AccountManager.class)
公共类MyActivityTest扩展ActivityInstrumentationTestCase2{
{
@嘲弄
公共活动我的活动;
@嘲弄
会计经理会计经理;
@以前
public void setUp()引发异常{
initMocks(this);
}
@试验
公开嘲笑{
mockStatic(AccountManager.class);
when(AccountManager.get(any(MyActivity.class))。然后return(AccountManager);
when(accountManager.getAccounts()).thenReturn(新帐户[]{});
MyActivity活动=新的MyActivity();
activity.onCreate();
assertEquals(0,activity.getAccounts().length);
}
@试验
不带嘲弄的公共无效(){
MyActivity活动=新的MyActivity();
activity.onCreate();
assertEquals(2,activity.getAccounts().length);
}
}

我也可以用一个答案来回答这个问题——只要这个答案不只是一个有根据的猜测,比如“扔Mockito吧!”你是如何在Android中运行PowerMock的?据我所知,在Dalvik运行时是不可能的
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(AccountManager.class)
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
{
    @Mock
    public MyActivity myActivity;

    @Mock
    AccountManager accountManager;

    @Before
    public void setUp() throws Exception{
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void mocking() {
        mockStatic(AccountManager.class);
        when(AccountManager.get(any(MyActivity.class))).thenReturn(accountManager);
        when(accountManager.getAccounts()).thenReturn(new Account[] {});
        MyActivity activity = new MyActivity();
        activity.onCreate();
        assertEquals(0, activity.getAccounts().length);
    }

    @Test
    public void withoutMocking() {
        MyActivity activity = new MyActivity();
        activity.onCreate();
        assertEquals(2, activity.getAccounts().length);
    }

}