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...
        }

}

Thanks so much!
Fernan.do


On Apr 7, 2:20 am, hackbod <[EMAIL PROTECTED]> 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" <[EMAIL PROTECTED]> 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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Android Internals" group.
To post to this group, send email to android-internals@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/android-internals?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to