views:

84

answers:

2

Hey there!

I have an Java class with a static final method getAll:

public static final Vector<Category> getAll(Context context, ContentValues where) {
    ArrayList<Integer> IDs = null;

    if(where != null && where.containsKey(DatabaseAdapter.KEY_PRODUCT)) {
        IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_PRODUCT_CATEGORY, new String[] { DatabaseAdapter.KEY_CATEGORY }, where);
    } else {
        IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_CATEGORIES, where);
    }

    Vector<Category> categories = new Vector<Category>();

    for(Integer id: IDs) {
        categories.add(Category.get(context, id));
    }

    return categories;        
}

Now I want to hand in null as a value for the where statemant so that it will just be ignored later on in the code. Anyway in the testcase for this method I have:

Vector<Category> categories = Category.getAll(context, null);

Which then in turn gives me a NoSuchMethodError. I don't know exactly why it does that. The only thing I could imagine is that the null I hand in would not match the signature of the above method. But how can I overcome this? I already thought of overloading. But this would just end in rewriting most of the code. At least when I do it, how I think.

Any suggestions on that?

Phil

P.S. This is the stack trace I get:

java.lang.NoSuchMethodError: com.sap.catalogue.model.Category.getAll
at com.sap.overture.test.model.CategoryTest.testGetAll(CategoryTest.java:59)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
+2  A: 

If the method did not exist at compile-time, then the code would not compile.

If you get NoSuchMethodError at run-time, then this suggests that the version of the Category class you are running against is different than the version of the Category class you are compiling against.

What is your setup like - is this class in the same project? Are you copying in JARs from another project?

matt b
+1 - but it doesn't just "suggest" this. AFAIK, there is no other plausible explanation.
Stephen C
The Category class is in the main project (android application). The tests sit in the test project that is directly related to the main application. It is somehow strange because all of the other tests pass without problems. Plus I don't do anything like changing the class at run-time. It is a plain model class. Holding data and the logic to manage the data.
philgiese
@Stephen I don't like to speak in terms of absolutes in the 0.1% chance I didn't think of something or that philgiese has some awkward setup :)
matt b
@philgiese have you tried cleaning both projects and re-compiling? Also is the method overloaded by any chance?
matt b
I have done a full clean on both projects without any change. The getAll() method is indeed overloaded. There is also a getAll() Method in the superclass OvertureItem. But the method in the superclass takes 3 parameters and not two.
philgiese
I added the stack trace I get to my original questions. Still don't have a clue why this happens.
philgiese
A: 

The real answer

So I now finally figured it out and it wasn't as obvious as I expected. I started wondering, when every new test case for any new method I wrote would give me the NoSuchMethodError. So I digged a little bit deeper and then, suddenly it came to my mind: "I changed the package name of the android application". I thought this would not make any difference to the test project as long as I kept the properties right in the AndroidManifest.xml but I was wrong!

In fact when your application package is named com.foo.bar.app, the package for your tests has to be named com.foo.bar.app.test! What happened was, that with my old configuration somehow the classes that sat in the bin/ folder were used. I thought, that they should have been deleted when I cleaned the project but they weren't. This way all of the older test cases would still pass and only the new ones would give me the NoSuchMethodError. After I deleted the bin/ folder manually I got a whole bunch of errors. I then renamed the package holding the test cases and did a full clean/ rebuild on the project et voilá everything is back to normal again.

Thanks for all the tips! I really appreciate your help that just kept me digging to the bottom of the problem. Hope this here will help anybody with the same problem in the future.

Phil

philgiese