Trying to implement some small test cases in my Android application, but at the moment i have some trouble getting the test class to find the fragment.
Here is my Activity that just create my fragment.
public class DetailActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent intent = getIntent();
Bundle bundle = new Bundle();
bundle.putParcelable("match", intent.getParcelableExtra("match"));
DetailFragment fragment = new DetailFragment();
fragment.setArguments(bundle);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, fragment, "test")
.commit();
}
}
}
And my fragment looks like this.
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity().getApplicationContext();
mh = getArguments().getParcelable("match");
int id = context.getResources().getIdentifier(
"c" + String.valueOf(mh.getChampionImage()),
"drawable", context.getPackageName()
);
}
@Override
public void onStart() {
super.onStart();
if (Utility.hasNetworkConnection(context)) {
FetchIcons fetchIcons = new FetchIcons();
fetchIcons.execute(mh);
}
}
...//Some Code
My test looks like this.
public class DetailFragmentTest extends ActivityUnitTestCase<DetailActivity> {
DetailActivity activity;
DetailFragment fragment;
public DetailFragmentTest() {
super(DetailActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
Intent intent = new Intent(new MockContext(), DetailActivity.class);
int[] i = {1, 2, 3, 4, 5, 6, 7};
MatchHistory mh = new MatchHistory("Win", "23", "42", "31", "23414", "321", 22, true, i);
intent.putExtra("match", mh);
startActivity(intent, null, null);
activity = getActivity();
fragment = (DetailFragment) activity.getSupportFragmentManager().findFragmentByTag("test");
}
public void testText() {
...//Some more code
}
}
Everything up to the part where i'm trying to receive the framgent works fine, but gives me NullPointerException as soon as i try to fetch it.
And my stacktrace doesn't shed much light on the problem.
java.lang.NullPointerException
at com.plushogskolan.chobi.matchhistory.DetailFragmentTest.setUp(DetailFragmentTest.java:34)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Been trying to solve it using several different methods, including just the ActivityInstrumentationTestCase2, but then i can't even get the Activity.
I'm clearly doing something wrong. But since not a single tutorial for unit testing in android have yielded any luck i can at least give it a shot here and see if anyone knows what the problem is.
Regards!