thej
thej

Reputation: 648

testing a class with out activity and services

this is my actual class I am writing test class below.

public class ReadFile {
private Scanner rx, tx;

public void openFile() {
    try {
        rx = new Scanner(new File("/sys/class/net/eth0/statistics/rx_bytes"));
        tx = new Scanner(new File("/sys/class/net/eth0/statistics/tx_bytes"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String readRxFile() {
    String rxData = "";
    while (rx.hasNext()) {
        rxData = rx.next();
    }

    return rxData;
}

public String readTxFile() {
    String txData = "";
    while (tx.hasNext()) {
        txData = tx.next();
    }
    return txData;
}

public void closeFile() {
    rx.close();
    tx.close();
}
}

this is test class. to test the read data.

public class Testreadrxfile extends TestCase {

public Testreadrxfile() {
    super();
}

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

}

ReadFile rf;

public void testappdata() {
   String str1 = rf.readRxFile();
    Assert.assertEquals("14081",str1 );
}

}

this is the error I am getting.

java.lang.NullPointerException
atcom.android.deviceintelligence1.             test.Testreadrxfile.testappdata(Testreadrxfile.java:26)
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 not activity or service test to use getactivity() or getservice() so what I should do to get out of this error . thanks for help in advance.

Upvotes: 0

Views: 116

Answers (2)

Mayank
Mayank

Reputation: 8852

yeah, rf is not initialised. In this kind of situation first thing to do is attach the debugger on the line you are getting error, and in most cases answer becomes obvious.

UPDATE

first your code didn't work because you didn't initialised rx, now your code doesn't work becasue you never called following function. In the function you are initialising your scanner objects. call this function from your readRxFile function and you should be good to go.

public void openFile() { 
    try { 
        rx = new Scanner(new File("/sys/class/net/eth0/statistics/rx_bytes")); 
        tx = new Scanner(new File("/sys/class/net/eth0/statistics/tx_bytes")); 
    } catch (Exception e) { 
        e.printStackTrace(); 
    } 
} 

public String readRxFile() { 
    String rxData = ""; 
    while (rx.hasNext()) { //eq. to null.haxNext()
        rxData = rx.next(); 
    } 

    return rxData; 
}

Upvotes: 0

ngesh
ngesh

Reputation: 13501

see your code.. rf is not initialised..

ReadFile rf; // not intialisd

public void testappdata() {
   String str1 = rf.readRxFile(); // equivalent to null.readRxFile();
    Assert.assertEquals("14081",str1 );
}

Upvotes: 1

Related Questions