Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hamcrest old version causes: java.lang.NoSuchMethodError: No static method allOf #357

Closed
rsavin opened this issue Jul 30, 2020 · 22 comments · Fixed by #436
Closed

Hamcrest old version causes: java.lang.NoSuchMethodError: No static method allOf #357

rsavin opened this issue Jul 30, 2020 · 22 comments · Fixed by #436

Comments

@rsavin
Copy link

rsavin commented Jul 30, 2020

Please consider making a Pull Request if you are capable of doing so.

Library Version:

3.5.0

Describe the Bug:

When I use the assertListItemCount(R.id.list, 5) method and some other methods from Barista I get the following exception:

java.lang.NoSuchMethodError: No static method allOf(Lorg/hamcrest/Matcher;Lorg/hamcrest/Matcher;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher; in class Lorg/hamcrest/CoreMatchers; or its super classes (declaration of 'org.hamcrest.CoreMatchers' appears in /data/app/com.myapp.android.debug.test-teuB7mkareqg4fXqftwOghQ==/base.apk!classes3.dex)
at com.schibsted.spain.barista.interaction.BaristaListInteractions.findRecyclerMatcher(BaristaListInteractions.kt:118)
at com.schibsted.spain.barista.assertion.BaristaListAssertions.assertListItemCount(BaristaListAssertions.kt:33)
...
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:288)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:282)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:764)

I did some investigation and the problem is that we have Hamcrest v2.2 in our project and Barista uses Hamcrest v1.3 which comes transitively from Espresso dependencies. If I downgrade Hamcrest to v1.3 in the project dependencies it works fine.

Is it possible to start using Hamcrest v2.2 in Barista to make it possible to use in project with the updated library? I can open a PR if it's fine for you to get updated.

Steps to reproduce the bug:

Expected Behavior:

@Sloy
Copy link
Member

Sloy commented Aug 3, 2020

@rsavin Hi Roman! Thanks for the report.
Are you able to reproduce this issue in the sample project from this repository, or any other project? How are you adding the Hamcrest dependency?
I tried adding androidTestImplementation("org.hamcrest:hamcrest:2.2") to the sample and to another internal project, and the build just fails with errors like

Duplicate class org.hamcrest.BaseDescription found in modules hamcrest-2.2.jar (org.hamcrest:hamcrest:2.2) and hamcrest-core-1.3.jar (org.hamcrest:hamcrest-core:1.3)

If both versions are not compatible, we won't be able to update Hamcrest until Espresso does it first. And even if the build would work, we shouldn't because we might break other projects out there not using 2.2.

@Sloy
Copy link
Member

Sloy commented Jul 1, 2021

Closing for inactivity

@Sloy Sloy closed this as completed Jul 1, 2021
@shuhart
Copy link

shuhart commented Jul 7, 2021

We got the same issue. Espresso 3.4.0 uses hamcrest-library 2.2, which causes compile errors due to incompatibility with harmcrest 1.3.

@rocboronat rocboronat reopened this Jul 7, 2021
@rocboronat
Copy link
Member

Hello @shuhart! 👋

Just to clarify: if you used the latest version of Espresso, you would have the same issue, right? And you are using Hamcrest 1.3, aren't you?

Maybe you should move from Hamcrest 1.3 to 2.2 and that's it?

@rocboronat
Copy link
Member

rocboronat commented Jul 7, 2021

Sorry! I didn't read you well. You are on 2.2 and Espresso is on 1.3.

Are you on 2.2 for any special reason? May you move to 1.3 or there's any special case 1.3 is not able to handle?

@shuhart
Copy link

shuhart commented Jul 7, 2021

We don't use harmcrest internally. The problem is that Barista depends on harmctest 1.3, that's why the only thing we can do is to downgrade either Espresso or harmcrest (using gradle hacks) or just get rid of Barista.

@rocboronat
Copy link
Member

Oh, if you don't use Hamcrest, why don't you just remove your Hamcrest dependency? And you could also remove the dependency on Espresso, and just depend on Barista. As Barista depends on both, it's the only dependency you need.

Please, give it a try :·)

@shuhart
Copy link

shuhart commented Jul 7, 2021 via email

@rocboronat
Copy link
Member

I think having dependencies you don't use is not too wise, but you choose. Thanks for the feedback, though! 👍

@Sloy
Copy link
Member

Sloy commented Jul 7, 2021

If you're not using Hamcrest, it does sound like a great idea. You should not declare dependencies that you're not going to use. If other libraries require Hamcrest, they will pull the right version already. That's how transitive dependencies work :)

@shuhart
Copy link

shuhart commented Jul 7, 2021 via email

@growse
Copy link
Contributor

growse commented Jul 7, 2021

@shuhart I think (someone correct me if I'm wrong) the suggestion is to remove the explicit dependency on Espresso, given that Barista already depends on it and will pull it into the tree.

The problem will come if you depend on anything in Espresso in a more recent version than that which is currently pulled in by Barista (v3.1.1 I think). Then it gets a bit awkward.

In my case, I ran into this issue, but then noticed that I'd explicit dependencies on espress which I didn't need. Removing them fixed it.

Is there an existing Barista issue for updating the espresso dependency for those who do need it?

@rocboronat
Copy link
Member

Now we're working on publishing the latest version of Barista to Android Central. Then, we'll go back to common things like using the latest Espresso version. Sorry for the delay!

By the way, in case @shuhart doesn't do anything special only the latest version of Espresso handles, removing the dependencies and relying on Barista just works 😄

Thanks for your comment, too! Happy you were in the same situation and fixed it just removing your Espresso dependency.

@shuhart
Copy link

shuhart commented Jul 7, 2021 via email

@Sloy
Copy link
Member

Sloy commented Jul 8, 2021

Hi @shuhart! Even the latest version of Espresso uses Hamcrest 1.3, so it's not an issue with Barista. Another library must be importing Hamcrest 2.2.
(According to https://mvnrepository.com/artifact/androidx.test.espresso/espresso-core/3.4.0)

You can check which dependency is pulling Hamcrest 2.2 by running a Gradle scan or using the dependencies task as explained here: https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html

If you remove Barista, you can still have the same error if you use any Espresso matcher that uses a Hamcrest method that doesn't exist in 2.2. I'm not sure that there's a fix for that :/

@shuhart
Copy link

shuhart commented Jul 8, 2021

espresso-contrib depends on accessibility-test-framework which depends on hamcrest-2.2

@shuhart
Copy link

shuhart commented Jul 8, 2021

We already have a temporary solution for that, I just wanted to point out the issue with you library (was not seeking a solution or advices on how to fix the issue).

@Sloy
Copy link
Member

Sloy commented Jul 8, 2021

Ah! Good to hear!
So the fix is to update all Espresso dependencies in Barista, that's easy :)

@LeMimit
Copy link

LeMimit commented Aug 18, 2021

Hello,

I got the same problem in my project. it appears that the version of hamcrest is declared strictly to version 2.2 by the last expresso.

Only solution, I found is to downgrade to version 3.3.0 of expresso instead using the version 3.4.0.

Do you have another solution ? Because this is annoying to use this old version of expresso.

Do you know when it will be fixed on your side ? (Update the version of expresso used by the library)

Thanks !

@c4augustus
Copy link

c4augustus commented Mar 23, 2022

This does not fix the essential problem which is actually Google's fault. Augmenting what @shuhart wrote above:

barista 4.2.0 depends on
....espresso-contrib 3.4.0 which depends on
........accessibility-test-framework 3.1 which depends on
............hamcrest library & core 2.2.
Notice that Maven shows accessibility-test-framework version 2.1 as a "new version" to 3.1, so obviously someone became aware of the problem, but they didn't fix espresso-contrib 3.4.0. Google fix your stuff!

So then the only solution for anything using espresso-contrib, including Barista, is to force the strict downgrade to accessibility-test-framework 2.1 which depends instead upon hamcrest core & library 1.3, the same version used by espresso-core 3.4.0

The following gradle code fixes it for me using Espresso 3.4.0 and Barista 4.2.0.

    androidTestImplementation ('com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework') {
        // !!! prevent 3.1 which pulls hamcrest 2.2, breaking espresso-core and Barista
        version { strictly '2.1' }
    }
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'

@RahulSDeshpande
Copy link

RahulSDeshpande commented Aug 2, 2022

So finally this dependency fixed all the errors:

androidTestImplementation "org.hamcrest:hamcrest:2.2"

💃🏼

@lannyf77
Copy link

@RahulSDeshpande why androidTestImplementation "org.hamcrest:hamcrest:2.2" would fix it?
If espresso 3.4.0 uses "org.hamcrest:hamcrest:2.2", it should have been 2.2.
or if espresso uses "org.hamcrest:hamcrest:1.3", why the 2.2 will help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants