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

Unable to mock javax.sql.DataSource using ByteBuddyClassImposteriser #127

Closed
dpeger opened this issue May 13, 2019 · 4 comments
Closed

Unable to mock javax.sql.DataSource using ByteBuddyClassImposteriser #127

dpeger opened this issue May 13, 2019 · 4 comments

Comments

@dpeger
Copy link

dpeger commented May 13, 2019

JMock-Version: 2.11.0
JVM-Version: OpenJDK 11.0.1

When trying to mock the javax.sql.DataSource interface with the ByteBuddyClassImposteriser, creation of the mocked instance fails due to a java.lang.ClassNotFoundException for class org.jmock.internal.CaptureControl.

Execution of this test

public class JMockTest {
    
    @Test
    public void tryToMockSqlDataSourceWithByteBuddy() {
        final Mockery mockery = new Mockery();
        mockery.setImposteriser(ByteBuddyClassImposteriser.INSTANCE);

        final DataSource dataSourceMock = mockery.mock(DataSource.class);
        
        mockery.assertIsSatisfied();
    }
}

Results in this exception to be thrown:

java.lang.IllegalStateException: Error invoking java.lang.invoke.MethodHandles$Lookup#defineClass
	at net.bytebuddy.dynamic.loading.ClassInjector$UsingLookup$Dispatcher$ForJava9CapableVm.defineClass(ClassInjector.java:1686)
	at net.bytebuddy.dynamic.loading.ClassInjector$UsingLookup.injectRaw(ClassInjector.java:1415)
	at net.bytebuddy.dynamic.loading.ClassInjector$AbstractBase.inject(ClassInjector.java:111)
	at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$UsingLookup.load(ClassLoadingStrategy.java:466)
	at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:100)
	at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:5623)
	at org.jmock.imposters.ByteBuddyClassImposteriser.proxyClass(ByteBuddyClassImposteriser.java:170)
	at org.jmock.imposters.ByteBuddyClassImposteriser.access$000(ByteBuddyClassImposteriser.java:41)
	at org.jmock.imposters.ByteBuddyClassImposteriser$1.apply(ByteBuddyClassImposteriser.java:100)
	at org.jmock.imposters.ByteBuddyClassImposteriser$1.apply(ByteBuddyClassImposteriser.java:96)
	at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
	at org.jmock.imposters.ByteBuddyClassImposteriser.proxy(ByteBuddyClassImposteriser.java:95)
	at org.jmock.imposters.ByteBuddyClassImposteriser.imposterise(ByteBuddyClassImposteriser.java:66)
	at org.jmock.Mockery.mock(Mockery.java:166)
	at org.jmock.Mockery.mock(Mockery.java:142)
	at jmock.datasource.JMockTest.tryToMockSqlDataSourceWithByteBuddy(JMockTest.java:19)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.NoClassDefFoundError: org/jmock/internal/CaptureControl
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.System$2.defineClass(System.java:2123)
	at java.base/java.lang.invoke.MethodHandles$Lookup.defineClass(MethodHandles.java:962)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at net.bytebuddy.dynamic.loading.ClassInjector$UsingLookup$Dispatcher$ForJava9CapableVm.defineClass(ClassInjector.java:1682)
	... 40 more
Caused by: java.lang.ClassNotFoundException: org.jmock.internal.CaptureControl
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 48 more
@dpeger dpeger changed the title Unable to mock javax.sql.DataSource using ByteBuddyImposteriser Unable to mock javax.sql.DataSource using ByteBuddyClassImposteriser May 13, 2019
@olibye
Copy link
Member

olibye commented May 16, 2019

Is this testcase available in github somewhere so I can reproduce it with:
git clone --branch failingBranch
mvn clean install

@olibye
Copy link
Member

olibye commented May 16, 2019

I ask, because from reading, it appears to be a class path issue? So I'm interested in the classpath you're using in groovy or maven or an IDE or whatever you're running tests in?

@dpeger
Copy link
Author

dpeger commented May 16, 2019

Here you go https://github.com/dpeger/jmock-issue127

Issue is reproducible in eclipse or by executing gradlew build

@olibye
Copy link
Member

olibye commented May 17, 2019

Thanks, this is a significant issue. The tests that check this case have not been running correctly.
I am working on a solution.

@olibye olibye added this to the 2.12.0 milestone May 18, 2019
olibye pushed a commit to olibye/jmock-library that referenced this issue May 18, 2019
olibye added a commit that referenced this issue May 18, 2019
Fix Synchroniser issue #121 and ByteBudderImposteriser ClassLoader issue #127
@olibye olibye closed this as completed May 18, 2019
@olibye olibye unpinned this issue May 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants