jeudi 5 février 2015

android unit test getStartedActivityIntent always null

I am new at unit test so I am testing a working app that I have to train.


I want to test an Intent that starts an activity at the click of a button.


My method in the test class is :



public void testFindButtonStartsProductDetailActivity (){
startActivity(mainIntent, null, null);
final Button findButton = (Button) getActivity().findViewById(com.apps4better.recycle4better.R.id.findButton);

findButton.performClick();

final Intent launchIntent = getStartedActivityIntent();
assertNotNull("launchIntent is null", launchIntent);
}


And the click method in my app looks like this :



public void onClick(View v) {
// TODO Auto-generated method stub
//check for network connection before anything
if (NetworkInspector.haveNetworkConnection(MainActivity.this)){
Log.d("Main activity", "giving product id = "+productIdTextField.getText().toString());
String a = productIdTextField.getText().toString();
long id = Long.valueOf(a).longValue();
Intent intent = new Intent(context, ProductDetailActivity.class);
intent.putExtra(TAG_PRODUCT_ID, id);
intent.putExtra("load_info", true);
startActivity(intent);
}
}


I think the problems comes from the network check that I perform before sending the Intent.


Is it possible that my test method never goes past the Network check? If yes, what it the appropriate solution and a good practice when unit testing ?


Aucun commentaire:

Enregistrer un commentaire