Robolectric 3.0与Facebook sdk 4.0

时间:2015-04-02 14:50:50

标签: android facebook-android-sdk robolectric android-testing

我试图让我的测试与Robolectric.setupActivity(LoginActivity.class);一起使用,我的班级使用facebook sdk 4.0进行登录。即使我添加FacebookSdk.sdkInitialize(this);如下:

public class LoginActivity extends FragmentActivity {

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        FacebookSdk.sdkInitialize(this);

        if (savedInstanceState == null) {
            fragmentManager = getSupportFragmentManager();
            ...
        }
    }

我的测试中出现以下错误:

java.lang.RuntimeException at LoginActivityTest.java:32
        Caused by: com.facebook.FacebookSdkNotInitializedException at LoginActivityTest.java:32

这是我的测试类:

@RunWith(CustomRobolectricRunner.class)
@Config(emulateSdk = 21, reportSdk = 21, constants = BuildConfig.class)
public class LoginActivityTest {

    private Button loginButton;
    private ShadowActivity loginActivityShadow;

    @Before
    public void setUp() throws Exception
    {
        LoginActivity activity = Robolectric.setupActivity(LoginActivity.class);

        loginButton = (Button) activity.findViewById(R.id.loginButton);
        loginActivityShadow = Shadows.shadowOf(activity);
    }

    @Test
    public void shouldStartFacebookActivityWhenLoginButtonIsClicked()
    {
        Intent intent = loginActivityShadow.getNextStartedActivity();
        loginButton.performClick();
        //assertThat(activity, new StartedMatcher(com.facebook.LoginActivity.class));
    }

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在致电FacebookSdk.sdkInitialize(this);之前,您需要致电setContentView(R.layout.activity_login);