Wednesday, 15 July 2015

Android espresso onData error

1. The onData we can get is this:

onData(allOf(is(instanceOf(Map.class)), hasEntry(equalTo("STR"), is("item: 50")))

2. However, you will never use Map for your data. You will use ProductVO.class or XXXVO.class.

3. Then you can't use "hasEntry" you have to use "hasProperty" such as:

onData(allOf(is(instanceOf(DeliveryVO.class)), hasProperty("deliveryReferenceId", is("1235"))))

4. However, you will get this error:

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;
at org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor(
at org.hamcrest.beans.PropertyUtil.getPropertyDescriptor(
at org.hamcrest.beans.HasPropertyWithValue.propertyOn(
at org.hamcrest.beans.HasPropertyWithValue.matchesSafely(
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(
at org.hamcrest.core.AllOf.matches(
at org.hamcrest.DiagnosingMatcher.matches(
at java.util.concurrent.Executors$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(

5. "java.beans.Introspector" is a core java class. As we know android uses java but it doesn't use the full version of java.

6. the reason we use "java.beans.Introspector" is, we are using Espresso for testing and it uses "Hamcrest" which use javaBeans.

7. Solution One:
     Get Hamcrest's source code and remove all javabeans and use openJavaBeans.  ericharlow already did this change, you can use the source code directly:

    However, you figure out how to exclude hamcrest-library.jar in gradle (we cause we don't directly use hamcrest in gradle, we use espresso and espresso bring it in.) we also need make sure you use the right version of hamcrest to match your espresso.
    Even you can solve all of these issues, we need compile it your self and put it into gradle (if you want put it into "libs", that is much easier)

   So, this could be a solution be not good one!!! let's see the solution two below.

8. Solution Two:

   Possible solution: Put java bean jar file into your project. But there is no such jar file, it is in the rt.jar file. If you put rt.jar in you will get error, because it is too much, it is the whole JRE.

   So we will copy the source files we need and put them into our project. you can get all source code of JRE from unzip it and you can get the java file from:

create the same package and copy it over. Because it depends on other files, so you need keep copying. you will endup with:

You will have to delete some code e.g. awt.image which is useless for us. (have to say this is a rubbish architecture design from sun, java bean is a highly abstract things can be used by all java package, awt.* stuff should just inside of awt package.  Due to awt is a part of core java, it is not a big issue. But if we observe it now, in terms of extract java bean, it is rubbish.)

You don't have to manually copy them I already did that and you can download the code from :

After copy, if you compile your code now, you will have this error:

    trouble processing "java/beans/BeanDescriptor.class":
    Ill-advised or mistaken usage of a core class (java.* or javax.*)

Because you attempt to include a core class (java.* or javax.*) in something other than a core library

And it is also ugly to put this code which is for testing into your project even not in "main". So I suggest:

Then download my code from and put it here or you can override the whole folder of "jdklib" by my folder.

Very importantly, you should put this input your gradle file: (inset of android{})

dexOptions {
    preDexLibraries = false    javaMaxHeapSize "4g"}

project.tasks.withType( {

Then in your module setting page:

Rerun your test, you should get:

No comments:

Post a comment