Sugar orm in unit test produced null pointer exception - android

What I'm trying to do is implement a unit test, but I need to clear the database before it.
The problem is that sometimes clearing database fails with NullPointerException.
Basicily I call deleteAll method on every DTO I have (for example BusinessUnit.deleteAll(BusinessUnit.class). And sometimes this fail (though not always).
Null pointer originates from SugarRecord's own method:
public static <T extends SugarRecord<?>> void deleteAll(Class<T> type) {
Database db = SugarApp.getSugarContext().getDatabase();
SQLiteDatabase sqLiteDatabase = db.getDB(); // exception is thrown here
sqLiteDatabase.delete(getTableName(type), (String)null, (String[])null);
}
What could have caused this error?

Let the sugarorm start up at first. You have first to let start the SugarOrm. This task needs some time. Therefore add the following:
public class ApplicationTest extends ApplicationTestCase<Application> {
#Override
public void setUp() throws Exception {
super.setUp();
// SugarORM need some time to start up, else tests will fail
Thread.sleep(3000);
}

Related

How to clean db with ProviderTestCase2 or RenamingDelegatingContext

I do not understand what exactly should I do in order to get a clean and a different db from the one that the app uses.
This is my test class:
public class SQLTest extends ProviderTestCase2{
private static String testDbPrefix = "unitTest_";
public SQLTest (){
super(MyContentProvider.class, MyContract.CONTENT_AUTHORITY);
}
#Override
#Before
public void setUp() throws Exception {
//setContext(InstrumentationRegistry.getTargetContext());
RenamingDelegatingContext context = new RenamingDelegatingContext(InstrumentationRegistry.getTargetContext(), testDbPrefix);
setContext(context);
super.setUp();
}
#Test
public void test1(){
//test logic
}
}
I noticed that it always runs on the db that the app uses, even though I'm using a both ProviderTestCase2 and RenamingDelegatingContext, which are supposed to ensure I'm running with a clean db.
Can anyone explain please what am I missing???
Thanks in advance!

Robolectric error org.fest.reflect.exception.ReflectionError: Unable to find method '$$robo$getData'

When i am wrtting
#Before
public void setUp() throws Exception {
Activity activity = Robolectric.buildActivity(ActivityMain.class).create().get();
}
and after running the test Its giving me error org.fest.reflect.exception.ReflectionError: Unable to find method '$$robo$getData' I am using eclipse and ant build for testing the robolectric test for android.
But this code is working fine with my test
#Test
public void testBasicResourceValue() throws Exception {
String helloFromActivity = new ActivityMain().getResources().getString(R.string.str_my_file);
assertThat(helloFromActivity, equalTo("newfile"));
}
so its confirmed that the program is able to get the AndroidManifest.xml
Activity activity =
Robolectric.buildActivity(ActivityMain.class).create().get(); this
line is not working with ANT build i dont know why? i heard
robolectric is stopped supporting ANT.So i found a another workaround
for this problem to get the shadow object of the activity by using
this code given below
#Before
public void setUp() throws Exception {
MyActivity activity = new MyActivity();
//Remember setContentView() has to call before you are trying to get any resource ID from the activity like Button,TextView etc other wise you will get the not shadow object for those views.
activity.setContentView(R.layout.warning);
Assert.assertNotNull(activity);
}

How can I use Active Android with an in memory database for unit tests using Robolectric?

As the title says. I am aware that there is a limited in memory database provided in robolectric. Is there any way to use this with Active Android? Under the default configuration, it appears that the database is cleared after all the tests are run, but not for each test.
I use greenDao - but the principle is the same.
My Application class initialises my DB (the DB has a name). For my tests I subclass Application (which allows Robolectric to call this version instead) and override the method that gets the DB name - and return null. This then means I create an in memory DB. As the Application creation is part of setUp then a new in memory DB is used for each test.
public class MyApplication extends android.app.Application {
#Override
public void onCreate() {
super.onCreate();
initialiseDB(getDatabaseName());
}
protected String getDatabaseName() {
return "regular-db-name";
}
private void initialiseDB(String dbName) {
// DB initialization
// one example would be:
Configuration.Builder builder = new Configuration.Builder(this);
builder.setDatabaseName(dbName);
ActiveAndroid.initialize(builder.create());
}
}
public class TestApplication extends MyApplication {
#Override
protected String getDatabaseName() {
// use fresh in memory db each time
return null;
}
}

Android ProviderTestCase2: Fails in Run mode, Passes in Debug mode

I'm testing database using ProviderTestCase2 super class. Here is my code snippet:
public class MyProviderTest extends ProviderTestCase2<MyProvider>{
private static MockContentResolver resolver;
private static IsolatedContext context;
public MetaDataProviderTest() {
super(MyProvider.class, Provider.AUTHORITY);
}
#Override
protected void setUp() throws Exception {
try{
super.setUp();
resolver = getMockContentResolver();
} catch(Exception e){
}
}
}
public void testfirst(){
Cursor cursor = resolver.query(ProviderContract.Channels.CHANNEL_URI,null,null,null,null);
....
}
}
When I debug the above code Im getting the passed result. When I run it, I get Null cursor implying there is no such table as channel. Please help in solving this. Where did I go wrong?
There was race condition. Introduced delay in Setup(). It's working fine. But I don't know whether this is the exact solution.
you should write the code :
MockContentResolver.addProvider(authority, yourprovider);
then have a try

ActivityInstrumentationTestCase2 and use of static finals: fields become null after first test

This is really looks like some magic is going on and I'm interested to understand why that happens :)
Here's the unit-test I have:
public class SelectThemeActivityTest
extends ActivityInstrumentationTestCase2<SelectThemeActivity> {
private final static int[] STATIC_ARRAY = { 0, 1, 2 };
public SelectThemeActivityTest() {
super("com.the7art.simplewallpaper", SelectThemeActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
// some array usage here - will throw NullPointerEcxeption on second test
// see description below
STATIC_ARRAY[0] = 2;
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testFirst() {
}
public void testSecond() {
}
public void testThird() {
}
}
If I run this test case the first test completes successfully and all the rest fail by throwing NullPointerException from setUp() - the line which tries to access STATIC_ARRAY.
What puzzles me even more is the fact that if I change the test case to extend AndroidTestCase instead of ActivityInstrumentationTestCase2, then all tests complete successfully! Magic! :-)
Also if I remove 'static' keyword from STATIC_ARRAY, tests succeed too.
So it's clear that something is modifying my STATIC_ARRAY by making it null between a test runs, most probably in tearDown() and that something has to do with ActivityInstrumentationTestCase2, but how to track that something? :-) Any ideas?
The reason is scrubClass() method called from super.tearDown(): google-groups discussion. A solution is - overriding this method.
Put a watch point on STATIC_ARRAY and see who modifies it, although there are not too many candidates (since the field is private, there is pretty much only one candidate, the class you just posted, so something is missing from the picture.

Categories

Resources