我是使用Robolectric框架进行单元测试活动并获得以下错误的解析初始化错误。
MyActivity.java
public class WelcomeActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// hide action bar
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.hide();
//Checking Current User
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null)
{
Intent i=new Intent(WelcomeActivity.this, ViewPagerStyleLandingActivity.class);
startActivity(i);
WelcomeActivity.this.finish();
}
setContentView(R.layout.actvity_welcome);
SharedPreferences pref = getSharedPreferences(Constants.SIGNUP_CHECK_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.PREF_KEY_FIRST_RUN, false);
editor.commit();
Button loginbut=(Button) findViewById(R.id.login);
Button registerbut=(Button) findViewById(R.id.register);
loginbut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,LoginActivity.class);
startActivity(i);
}
});
registerbut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,RegisterActivity.class);
startActivity(i);
}
});
}
}
并将测试用例设为
@RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest {
private WelcomeActivity activity;
private Button loginbut;
@Before
public void setup() {
activity = Robolectric.buildActivity(WelcomeActivity.class).create().get();
loginbut = (Button) activity.findViewById(R.id.button);`}
@Test
public void checkActivityNotNull() throws Exception {
assertNotNull(activity);
}
@Test
public void shouldHaveButtonThatSaysAudit() throws Exception{
assertThat((String) loginbut.getText(), equalTo("Login"));
}
@Test
public void pressingLaunchButtonForSecondActivity() throws Exception {
String resultValue = "Testing Text";
assertNotNull(loginbut);
loginbut.performClick();
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
assertThat(resultValue, equalTo(startedIntent.getStringExtra("result")));
}
private ShadowContextWrapper shadowOf(WelcomeActivity activity2) {
return null;
}
@After
public void tearDown() throws Exception {
// Nothing Here
}
}
现在收到错误 使用jUnit
运行时