This question already has an answer here:
I have started activity testing following Official documentation at http://ift.tt/1PiIvaq
But I get a NullPointerException when trying to get a reference to the view in the Activity.
My MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Button changeText = (Button)findViewById(R.id.changeText);
changeText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((TextView)findViewById(R.id.inputField)).setText("Changed Text");
}
});
Button switchActivity = (Button)findViewById(R.id.switchActivity);
switchActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Bundle args = new Bundle();
args.putString("input",((TextView)findViewById(R.id.inputField)).getText().toString());
intent.putExtras(args);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Layout :
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.chandrak.pocespresso.MainActivity"
tools:showIn="@layout/activity_main">
<EditText
android:id="@+id/inputField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter text!!"
/>
<Button
android:id="@+id/changeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text"
/>
<Button
android:id="@+id/switchActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch Activity"
/>
</LinearLayout>
TestCase:
public class MainActivityUICompTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mMainActivity;
private Button mChangeTextBtn;
public MainActivityUICompTest() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(true);
mMainActivity = getActivity();
mChangeTextBtn = (Button) mMainActivity.findViewById(R.id.changeText);
}
public void testPreconditions() {
assertNotNull("mMainActivity is Null", mMainActivity);
assertNotNull("mChangeTextBtn is Null", mChangeTextBtn);
}
public void testButtonLayout() {
final View decorView = mMainActivity.getWindow().getDecorView();
assertOnScreen(decorView, mChangeTextBtn);
final ViewGroup.LayoutParams layoutParams = mChangeTextBtn.getLayoutParams();
assertNotNull(layoutParams);
assertEquals(WindowManager.LayoutParams.WRAP_CONTENT, mChangeTextBtn.getWidth());
assertEquals(WindowManager.LayoutParams.WRAP_CONTENT, mChangeTextBtn.getHeight());
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
}
Exceptions for testPreConditions and testButtonLayout :
java.lang.NullPointerException
at com.example.chandrak.pocespresso.MainActivityUICompTest.setUp(MainActivityUICompTest.java:28)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
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:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
java.lang.NullPointerException
at com.example.chandrak.pocespresso.MainActivityUICompTest.setUp(MainActivityUICompTest.java:28)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
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:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Aucun commentaire:
Enregistrer un commentaire