thej
thej

Reputation: 648

error in testing service class

java.lang.NullPointerException
at com.android.deviceintelligence1.test.Testappdata.testappd(Testappdata.java:29)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

this is error after running my test class.in error log.

public class Testappdata extends ServiceTestCase<MainService> {

    public Testappdata() {
        super(MainService.class);
    }

    protected void setUp() throws Exception {
        try {
            super.setUp();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    MainService main;

    public void testappd() {
        main = getService();
        String Package = "com.android";
        Assert.assertNotNull(main.appData(Package));

    }

this is my test class.to test service to get data value.

public class MainService extends Service {
private final static String TAG = "Device Intelligence";
double lat = 0.0;
double lng = 0.0;
List<Address> addresses = null;
String addre;
Handler mHandler = new Handler();
String lPackAgeName, packAgeName = "";
public static DataHelper dh;

public String appData(String pName) {
    PackageManager pm = getPackageManager();
    try {

        Method getPackageSizeInfo = pm.getClass().getMethod(
                "getPackageSizeInfo", String.class,
                IPackageStatsObserver.class);
        getPackageSizeInfo.invoke(pm, pName,
                new IPackageStatsObserver.Stub() {
                    public void onGetStatsCompleted(PackageStats pStats,
                            boolean succeeded) throws RemoteException {
                        data_value = String.valueOf(pStats.dataSize / 1024)
                                + "kb";
                        Log.d("bugs", "datavalue: " + data_value);
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return data_value;
}

this is my service class.so how can I clear my error.is ther any changes needed in test class to get out of the error .thanks for help in advance.

Upvotes: 0

Views: 334

Answers (1)

yorkw
yorkw

Reputation: 41126

I would assume NullPointerException comes from this line:

... ...

public void testappd() {
  main = getService(); // <-- Wrong at here, this will not return a testable service
  String Package = "com.android";
  Assert.assertNotNull(main.appData(Package)); // <-- throw NullPointerException
}

... ...

The correct way to get a testable service is by calling either startService(Intent) or bindService(Intent) in your ServiceTestCase class, check out the API here:

The test case waits to call onCreate() until one of your test methods calls startService(Intent) or bindService(Intent). This gives you an opportunity to set up or adjust any additional framework or test logic before you test the running service.

Assume you have a proper start service properly, you should write you test code something like this:

startService(new Intent(getContext(), MainService.class));
main = getService();
... ...

Hope this helps.

Upvotes: 2

Related Questions