Group testing of realistic Android classes. Test environment, life cycle and answers

It seems that the number of posts discussing how unit test completely unrealistic things don't end there.

An abundance of textbooks, videos, etc. describes what unit tests are and how you do them. However, it seems that there are not many (if any) resources that describe how to verify something real.

In the end, in reality, the β€œunits” we are testing are usually much more complicated than a method that accepts input and produces a result.

I am currently working with Android and have been learning how to unit test my application. My application consists mainly of views and server requests. You press the x button and change the displayed view. You press the y button, and it downloads data from the server and populates the list.

Below is some source code. I essentially put together an example setup that demonstrates things that are confusing (for me). What I find conceptually difficult for unit test.

public class ChainActivity extends FragmentActivity {

private PRFragmentTabHost mTabHost;

public GetChainResponse.Data responseData;

Integer chainId; //ref to chain we are getting - passed in


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get the chain id we are getting
    Bundle extras = getIntent().getExtras();
    chainId = extras.getInt("chainId");

    setContentView(R.layout.activity_base);

    //Set up the tabs
    mTabHost = (PRFragmentTabHost) findViewById(android.R.id.tabhost);

    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("Details").setIndicator("Details", null), ChainDetailsFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Pictures").setIndicator("Pictures", null), PicturesFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("Cats").setIndicator("Cats", null), CatsFragment.class, null);

}


@Override
public void onStart() {
    super.onStart();

    //Initiate the data load
    loadChainData();
}


//Method loads the chain data
public void loadChainData(){

    PRAPIInterface apiService = ApiService.getInstance();

    Integer limit = 4;

    apiService.getChain(chainId, limit, new Callback<GetChainResponse>() {

        @Override
        public void success(GetChainResponse pr, Response response) {

            lastData = System.nanoTime();

            //Save the response data
            responseData = pr.data;

            //Get the current tab and pass the loaded data to it
            String currentTabTag = mTabHost.getCurrentTabTag();
            DataLoadedInterface currentTab = (DataLoadedInterface) getSupportFragmentManager().findFragmentByTag(currentTabTag);
            currentTab.dataLoaded(responseData, false);
        }


        @Override
        public void failure(RetrofitError retrofitError) {
            // Log error here since request failed
            Log.w("Failed", "Failed" + retrofitError.getUrl());
            Log.w("Failed", "Failed" + retrofitError.getBody());
        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Fragment fragment = getSupportFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());

    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

}

So, I know Roboeletric, Robotium, etc. and other libraries that are available for testing on Android. However, I am looking for conceptual advice.

Android provides ActivityUnitTestCase

I can subclass this and set up a test for my activity.

Part 1

, onCreate, , mTabHost , , .

, , . "", , -, .

2

onStart. . . . , onStart .

loadChainData , , , Boolean true , .

, loadChainData '. . , , - loadChainData , . , , Android (-..?) , .

3

loadChainData . , . , .

singleton ApiService. , , . , , (, ), , , , , .

, . . . , , .. - , , ?

, , ActivityUnitTestCase.

4

, onActivityResult. , . . , - ActivityUnitTestCase.

, , , , onActivityResult.. . , , - . .

..

-, , , , . , " 2, 4 ":)

, . :) .

+4
1

onCreate

: - AndroidTestCase, , Bundle.
( , , )
( loadChainData(), chainId )

, , espresso Acceptance .

onStart

, . getChain. , , ?
 ( Activity onStart - [, MVC ] , .
 ( Volley in Activity, , , [ ui]).

( ), Espresso, , .

onActivityResult

, Robolectric Mockito mockito, , onActivityResult: - .

, , , , , SOLID . , Robolectric, InstrumentationTests Espresso, .

, , - - , , , .

0

All Articles