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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
chainId = extras.getInt("chainId");
setContentView(R.layout.activity_base);
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();
loadChainData();
}
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();
responseData = pr.data;
String currentTabTag = mTabHost.getCurrentTabTag();
DataLoadedInterface currentTab = (DataLoadedInterface) getSupportFragmentManager().findFragmentByTag(currentTabTag);
currentTab.dataLoaded(responseData, false);
}
@Override
public void failure(RetrofitError retrofitError) {
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 ":)
, . :) .