java - SQLiteOpenHelper test class for android -


i trying write unit test accessor class sqlite database in android developer. when run test class, following error:

java.lang.runtimeexception: method getwritabledatabase in android.database.sqlite.sqliteopenhelper not mocked. see http://g.co/androidstudio/not-mocked details.  @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java) @ com.example.jack.app4.helper.charityhelper.insertdata(charityhelper.java:54) @ helper.testcharityhelper.testinsertandselectall(testcharityhelper.java:63) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.junit.internal.runners.statements.runafters.evaluate(runafters.java:27) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:325) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:78) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.mockito.internal.runners.junit45andhigherrunnerimpl.run(junit45andhigherrunnerimpl.java:37) @ org.mockito.runners.mockitojunitrunner.run(mockitojunitrunner.java:62) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:117) @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:42) @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:262) @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:84) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:147) 

the problem instance of charityhelper create in onsetup of test null, can't work out how fix this. appreciated.

my accessor class test:

/**  * accessor class charity table.  *  * created jack on 07/09/2017.  */  public class charityhelper extends sqliteopenhelper {      private static final string database_name = "example.db";     private static final string table_name = "charity";     private static final string id = "id";     private static final string charity_name = "charityname";     private static final string charity_number = "charitynumber";     private static final string postcode= "postcode";     private static final string charity_type= "charitytype";       public charityhelper(context context) {         super(context, database_name, null , 1); //        sqlitedatabase db = this.getwritabledatabase();     }      @override     public void oncreate(sqlitedatabase db) {         db.execsql("create table " + table_name + " (id integer primary key autoincrement, charity_name text, charity_number integer, postcode varchar, genre varchar)");     }      @override     public void onupgrade(sqlitedatabase db, int i, int i1) {         db.execsql("drop table if exists " + table_name);         oncreate(db);     }       /**      * insert record charity table.      * @param charity - charity inserted      * @return boolean - whether insert successful or not.      */     public boolean insertdata(charity charity) {         sqlitedatabase db = this.getwritabledatabase();          contentvalues contentvalues = new contentvalues();         contentvalues.put(charity_name, charity.getname());         contentvalues.put(charity_number, charity.getnumber());         contentvalues.put(postcode, charity.getpostcode());         contentvalues.put(charity_type, charity.getcharitytype().tostring());          long result = db.insert(table_name, null, contentvalues);          return result != -1;     }       /**      * method return list of charities in db.      * return empty list if no record found.      * @return list<charity>      */     public list<charity> selectall() {         sqlitedatabase db = this.getreadabledatabase();          cursor cursor = db.rawquery("select * " + table_name, null);          return getcharitieslistfromcursor(cursor);     }       /**      * method return list of charities in db of charity type passed in.      * return empty list if no record found.      * @return list<charity>      */     public list<charity> selectbycharitytype(charity.charitytype charitytype) {         sqlitedatabase db = this.getreadabledatabase();          cursor cursor = db.rawquery("select * " + table_name + " genre = ?", new string[] {charitytype.tostring()});          return getcharitieslistfromcursor(cursor);     }      /**      * takes cursor db query , convert list of charities.      * @param cursor - passed in cursor      * @return list charity      */     private list<charity> getcharitieslistfromcursor(cursor cursor) {         list<charity> charitylist = new arraylist<>();          while (cursor.movetonext()) {             charitylist.add(new charity(                     cursor.getstring(cursor.getcolumnindex(charity_name)),                     cursor.getlong(cursor.getcolumnindex(charity_number)),                     cursor.getstring(cursor.getcolumnindex(postcode)),                     charity.charitytype.valueof(cursor.getstring(cursor.getcolumnindex(charity_type)))             ));         }         return charitylist;     }  } 

my test class:

 /**  * test class charity helper  * created jack on 10/09/2017.  */  @runwith(mockitojunitrunner.class) public class testcharityhelper {      private charity testcharity;     private charityhelper charityhelper;      private final string testcharityname = "testname";     private final long testcharitynumber = 1234l;     private final string testcharitypostcode = "co10 8np";     private final charity.charitytype testcharitytype = charity.charitytype.animal;      @mock     sqlitedatabase dbmock;      @mock context mockcontext;      @before     public void onsetup() {          charityhelper = new charityhelper(mockcontext);         testcharity = new charity(testcharityname,testcharitynumber,testcharitypostcode,testcharitytype);     }      @after     public void teardown() throws exception{         charityhelper.close(); //        super.teardown();     }       @test     public void testinsertandselectall() {         charityhelper.insertdata(testcharity);          list<charity> returnedcharitylist = charityhelper.selectall();          asserttrue(returnedcharitylist.contains(testcharity));      } } 

in setup method need use context returned renamingdelegatingcontext() method. need extend androidtestcase class

public class testcharityhelper extends androidtestcase{   .  .  private renamingdelegatingcontext mockcontext;    public void setup() throws exception {     super.setup();       final string prefix = "test";     mockcontext = new renamingdelegatingcontext(getcontext(),prefix);      charityhelper = new charityhelper(mockcontext);     testcharity = new charity(testcharityname,testcharitynumber,testcharitypostcode,testcharitytype); } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -