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

mockito 2.0.14 fails to mock jetty httpclient #233

Closed
christophsturm opened this issue Jun 18, 2015 · 14 comments
Closed

mockito 2.0.14 fails to mock jetty httpclient #233

christophsturm opened this issue Jun 18, 2015 · 14 comments
Labels
Milestone

Comments

@christophsturm
Copy link

this fails:

     import org.eclipse.jetty.client.HttpClient;
    HttpClient httpClient = mock(HttpClient.class);

(jetty 9.2.10.v20150310)

with mockito 2.0.2-beta i can mock that class without a problem.

stacktrace:

java.lang.IllegalAccessError: tried to access class org.eclipse.jetty.util.component.ContainerLifeCycle$Managed from class org.eclipse.jetty.client.HttpClient$MockitoMock$362486671
    at org.eclipse.jetty.client.HttpClient$MockitoMock$362486671.(Unknown Source)
    at sun.reflect.GeneratedSerializationConstructorAccessor2.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45)
    at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
    at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:14)
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:27)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.mock(Mockito.java:1392)
    at org.mockito.Mockito.mock(Mockito.java:1270)
@bric3
Copy link
Contributor

bric3 commented Jun 18, 2015

Is this an OSGI package ?

@bric3
Copy link
Contributor

bric3 commented Jun 18, 2015

Also a general principle when mocking is to not mock types you don't own, there's several reasons for that see that wiki page.

However the issue is probably true. But I cannot reproduce locally, do you have additional informations like jetty version, jvm version (OpenJDK, hotspot, J9, ...) ?

@christophsturm
Copy link
Author

yeah, i know about that rule. this is from a really tiny method and i just want to check that it invokes the start method.

oracle jdk8_11, jetty 9.2.10

@raphw
Copy link
Member

raphw commented Jun 18, 2015

The Managed inner class is package-private: http://grepcode.com/file/repo1.maven.org/maven2/org.eclipse.jetty/jetty-util/9.1.3.v20140225/org/eclipse/jetty/util/component/ContainerLifeCycle.java#ContainerLifeCycle.Managed

This class should never be accessible from org.eclipse.jetty.client.HttpClient or any mock which both live in another package. I assume that you have a version clash on your class path. Creating a mock can trigger lazy class path resolutions that are not even triggered in your production code via the class introspection that is required to create the mock.

@christophsturm
Copy link
Author

ok but the HttpClient class can be instantiated just fine by calling its constructor

client = new HttpClient();

@christophsturm
Copy link
Author

also the managed enum is only used inside ContainerLifeCycle.

@raphw
Copy link
Member

raphw commented Jun 18, 2015

I now see what the problem is. Byte Buddy overrides the public method ContainerLifeCycle::addBean(Object o, Managed managed) in order to allow mocking the method call. For this, the mock references the Managed class which is however package-private.

While this is a rather poorly designed API as the method should itself be package-private as it cannot be used outside of the package, Byte Buddy should be smart enough to figure this out. It is a small fix, I have it ready some time this week. Thanks for reporting!

@christophsturm
Copy link
Author

thanks. I agree that the package structure and api design of jetty is a bit strange.

@bric3
Copy link
Contributor

bric3 commented Jun 18, 2015

@raphw Thanks for the investigation

@bric3 bric3 added the bug label Jun 18, 2015
@bric3 bric3 added this to the 2.0 milestone Jun 18, 2015
@bric3
Copy link
Contributor

bric3 commented Jun 18, 2015

@christophsturm Thanks for the feddback too, note mockito 2.0.0 is still in beta phase

@christophsturm
Copy link
Author

sure! we will just continue to use 2.0.2-beta for now which works great.

@bric3
Copy link
Contributor

bric3 commented Jun 18, 2015

You can use a more recent beta 2.0.8-beta, the last version before switching to bytebuddy

@raphw
Copy link
Member

raphw commented Jun 18, 2015

Byte Buddy 0.6.9 is released which should fix the issue. Sorry for the hick-up. (In JCenter, currently synchronizing to Maven Central.)

@bric3
Copy link
Contributor

bric3 commented Jun 18, 2015

No problem, you were blazingly fast !

bric3 added a commit to bric3/mockito that referenced this issue Jun 18, 2015
…ublic parent class having a non public types in signature
bric3 added a commit to bric3/mockito that referenced this issue Jun 18, 2015
…ublic parent class having a non public types in signature

Reformated a bit the generator for improved readability
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants