I am having trouble implementing a JUnit test in my android studio project. What I have done is make a class extending AppCompatActivity called SellerMainPage. On this page, I have a TextView. I would like to check that the page opens up correctly and that the TextView is set to the right value. But I get a NullPointerException thrown in the SetUp() function coming from setting the TextView to the seller_main_page.xml file that corresponds to the SellerMainPage.java file.
The line
tv = (TextView) page.findViewById(R.id.textView9);
is where I think the problem is coming from. I have checked that this is indeed the TextView from the .xml document, so I do not know why I get the error.
SellerMainPageTest.java
package com.example.cristiannavarrete.my_shopping; import android.test.ActivityInstrumentationTestCase2; import android.widget.TextView; import junit.framework.TestCase; import org.w3c.dom.Text; import java.util.ArrayList; /** * Created by Cristian Navarrete on 12/1/2015. */ public class SellerMainPageTest extends ActivityInstrumentationTestCase2<SellerMainPage> { private SellerMainPage page; private TextView tv; @SuppressWarnings("deprecation") public SellerMainPageTest() { super("com.example.cristiannavarrete.my_shopping.SellerMainPage", SellerMainPage.class); } @Override protected void setUp() throws Exception { super.setUp(); page = this.getActivity(); tv = (TextView) page.findViewById(R.id.textView9); } public void testPreconditions() { assertNotNull("page is null", page); assertNotNull("tv is null", tv); } }
Here is the corresponding xml file
seller_main_page.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/sellerName"> <RelativeLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:text="@string/addProduct" android:id="@+id/addProduct" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/viewFinances" android:id="@+id/seeFinances" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentEnd="true" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/addProduct" android:layout_centerHorizontal="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/sellerInventory" android:id="@+id/textView9" android:layout_gravity="center_horizontal" android:textStyle="bold" android:textColor="#4aa260" android:textSize="25dp" /> <ScrollView android:layout_width="match_parent" android:layout_height="365dp" android:id="@+id/scrollView" android:layout_gravity="center_horizontal" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sellerListScroll"></LinearLayout> </ScrollView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/goBack" android:id="@+id/sellerGoBackButton" /> </LinearLayout> </RelativeLayout>
Here is my Log:
java.lang.NullPointerException at com.example.cristiannavarrete.my_shopping.SellerMainPageTest.setUp(SellerMainPageTest.java:30) 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:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Can someone help explain this to me?
No comments:
Post a Comment