I have a class called CallWcfTask that extended from extends AsyncTask<Void, Void, Void>
. I have declared these fields:
private String _CiName;
private CallWcfCallBack wcfCallBack;
private SQLiteDatabase _Db;
private List<String[]> infoList = new ArrayList<>();
in doInBackground method I have fetched data from my service and i put data into List<String[]>
.
protected Void doInBackground(Void... voids) {
try {
String s = EsupFactory.getConfigString(SETTING_ROOT_CI) + "GetCI";
JSONObject jsonObj = HttpUtil.requestJson(s, HttpUtil.HttpMethod.Post,
new JSONObject().put("PCiName", _CiName).toString());
JSONArray jCiArray = jsonObj.getJSONArray("GetCIResult");
if (jCiArray != null || !jCiArray.isNull(0)) {
for (int i = 0; i < jCiArray.length(); i++) {
try {
JSONObject tmpobj = jCiArray.getJSONObject(i);
String TmpId = String.valueOf(tmpobj.getInt("Id"));
String TmpTitle = tmpobj.getString("Title");
infoList.add(new String[]{TmpId, TmpTitle});
} catch (JSONException e) {
e.printStackTrace();
}
}
insert(infoList);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
and finally i have inserted into my db by invoking insert method.
public boolean insert(List<String[]> infoList) {
for (String[] value :
infoList) {
ContentValues insertValues = new ContentValues();
insertValues.put("ID", Integer.valueOf(value[0]));
insertValues.put("Title", value[1]);
long insert = _Db.insert(_CiName, null, insertValues);
if(insert == -1)
return false;
}
return true;
}
Now, I want to do unit test for insert method.I write blow test:
@RunWith(JUnit4.class)
public class CallWcfTaskTest extends TestCase {
CallWcfTask wcfTask;
List<String[]> strings;
@Before
public void onCreate() {
System.out.println("Test Started");
strings = new ArrayList<>();
for (int i = 0; i < 30; i++) {
String[] testlist = {i+"", "ali"+i};
strings.add(testlist);
}
wcfTask = new CallWcfTask();
}
@Test
public void insertTest() {
assertEquals(true,wcfTask.insert(strings));
}
}
but when I am running this test,I got this error:
Test Started
java.lang.NullPointerException
at com.safarayaneh.engineer.task.CallWcfTask.insert(CallWcfTask.java:79)
at com.safarayaneh.engineer.task.CallWcfTaskTest.insertTest(CallWcfTaskTest.java:33)
into my insert method, i am working with SQLiteDatabase _Db
. how could i test my method in my scenario?
***************** Edit *****************
I have opened my db from ClsCI class constructor:
public ClsCI(String pCiName, Context pcontext) {
datasource = new DataSource(pcontext);
dbhelper = new DatabaseHelper(pcontext);
_Db = dbhelper.getWritableDatabase();
_CiName = pCiName;
_Context = pcontext;
}
and i have executed CallWcfTask from this class and I have passed this _Db as a parameter to my task.