Sugar ORM needs records to be saved every time while unit testing?
Asked Answered
B

1

6

I am using Sugar ORM for db and Robolectric for unit testing. To use Sugar ORM with Robolectric i followed this question.

Where i put my code below in to startEverTestSugarAppAsFirst method and run.

new PersonHandler(new Activity()).insertPeople();
Person p = Person.findById(Person.class, 1);
System.out.println(p.getName());

It prints the name of the first record. So far so good.

But if i comment out //new PersonHandler(new Activity()).insertPeople(); and run again no records are returned and null pointer exception is given. I thought that Sugar ORM stored records at the first run to somewhere and i can access to these records at the second time.

On the other hand, if I run the emulator where i put the code below in my main activity's onCreate method and in the second run with commenting out the first line I can see the first record printed in the log.

new PersonHandler(this).insertPeople();
Person p = Person.findById(Person.class, 1);
Log.v("person", p.getName());

So, Does Sugar ORM delete db after unit test ends? or where it saves the db so i can use it over and over again?

Bigner answered 20/3, 2016 at 11:57 Comment(0)
D
1

It is not SugarORM but Robolectric. It creates temporary database every test so there is no hidden dependency between them.

This is good thing, your unit tests should be successfully run not dependent on order in which they are run

Dropwort answered 23/3, 2016 at 22:14 Comment(3)
I can use sqlite db with db = SQLiteDatabase.openOrCreateDatabase(filePath, null); command in robolectic unit test. Is there a way to connect sugar orm with a specified db path?Bigner
#24226041Dropwort
I know this post. But answers are about copying local db to /data/data/... folder where emulator can access it. I need to use local db for unit testing. Because, i already have db at /data/data/... location. But sugar orm code in the unit testing does not look at this location and use it.Bigner

© 2022 - 2024 — McMap. All rights reserved.