3 messages in com.googlegroups.android-internals[android-internals] Re: Creating a Mo...
FromSent OnAttachments
fernan.do05 Apr 2008 12:44 
hackbod06 Apr 2008 23:20 
fernan.do28 Apr 2008 05:01 
Subject:[android-internals] Re: Creating a MockContext() to test android
From:fernan.do (fern@gmail.com)
Date:04/28/2008 05:01:47 AM
List:com.googlegroups.android-internals

Hi hackbod, Thanks for your reply and sorry for the delay in answering. Your response makes perfect sense, but as you imagine I do not really want to create implementations for all the mock methods. I would much rather use a real context in my JUnit tests. You mentioned I sould get a real context and use that. Do you know how I can pass or get a real context to my JUnit class DbHelperTest?

public class DbHelperTest extends TestCase {

/** * Set-up code for tests */ private MockContext context=new MockContext(); DbHelper mDbHelper=new DbHelper(context);

public void testCreateDatabase (){ //What other tests can I do? can I check the database is created? try { mDbHelper.open(); } catch (SQLException ex) { fail(ex.getMessage()); }

//Database should have been created, proceed to check this is the case //but I can't check without calling the name... }

}

On Apr 7, 2:20 am, hackbod <hack@gmail.com> wrote:

A MockContext is a completely stubbed out context, so all of the methods on it throw UnsupportedException(). If there is some specific piece of functionality on Context that you want to mock, you make a subclass of MockContext and hand that to the code to be tested, allowing it to call back on your mock implementation.

For your specific case of openDatabase(), you will need to either make a subclass that implements that method as you desire (for example making an in-memory database), or get a real context and use that.

On Apr 5, 12:44 pm, "fernan.do" <fern@gmail.com> wrote:

Hi Everyone, I am trying to test my database CRUD methods. To do so I need to create a Context since Android's openDatabase and so on are invoked from the Context class. I was sure that one way to do so was to create a MockContext() and use that to invoke the mehtods. This is not working, the following piece of code gives me an UnsuportedOperationException:

private MockContext context=new MockContext(); mDb = context.openDatabase(DATABASE_NAME, null);

I am not sure why this is happening, since on the references pages these methods seem to be supported but since we do not have access to the source I am not really sure what is happening. Any one has run into a problem like this?

Thanks!