MockitoException with Maven but not with Eclipse

915 views
Skip to first unread message

Dave

unread,
Jan 21, 2011, 5:45:53 PM1/21/11
to mockito

I've run into a strange problem that I can't seem to solve. I use
Eclipse to develop my code and run unit tests and then use Maven to
build my deployment packages. Up until recently, everything was
running fine. I recently did a bunch of upgrades, including java,
maven, and mockito (1.8.4 to 1.8.5).

Now when I run unit tests from within Eclipse, all my tests execute
fine with no errors. However, when I run my tests with Maven, any
test that tries to mock a package private class pukes with the
exception below. The test class is in the same package as the class
being mocked.

Anyone know why this is happening?

org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class
zedi.pacbridge.controls.ControlStatusDelegate
Mockito can only mock visible & non-final classes.
If you're not sure why you're getting this error, please report to the
mailing list.
at
zedi.pacbridge.controls.ResponseStrategyTestCase.setUp(ResponseStrategyTestCase.java:
30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:
122)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
86)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:
88)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:
51)
at org.junit.internal.runners.JUnit4ClassRunner
$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:
42)
at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:
104)
at
org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:
147)
at
org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:
98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.ProviderFactory
$ClassLoaderProxy.invoke(ProviderFactory.java:101)
at $Proxy0.invoke(Unknown Source)
at
org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:
139)
at
org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:
82)
at
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:
81)
Caused by: org.mockito.cglib.core.CodeGenerationException:
java.lang.reflect.InvocationTargetException-->null
at
org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:
238)
at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
at
org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:
93)
at
org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:
50)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:54)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:45)
at org.mockito.Mockito.mock(Mockito.java:921)
at
org.mockito.internal.configuration.DefaultAnnotationEngine.processAnnotationOn(DefaultAnnotationEngine.java:
61)
at
org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:
36)
at
org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:
85)
at
org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:
40)
at org.mockito.MockitoAnnotations.scan(MockitoAnnotations.java:112)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:
95)
... 27 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:
385)
at
org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:
220)
... 40 more
Caused by: java.lang.IllegalAccessError: class
zedi.pacbridge.controls.ControlStatusDelegate$
$EnhancerByMockitoWithCGLIB$$a9b316b2 cannot access its superclass
zedi.pacbridge.controls.ControlStatusDelegate
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
... 45 more

Brice Dutheil

unread,
Jan 21, 2011, 6:28:51 PM1/21/11
to moc...@googlegroups.com
Hi Dave

Eclipse bootstrasps the tests differently than maven. If the tests pass in Eclipse, there's something going wrong with the maven environement.
I heard there was some problem with a recent update of surefire, which is invoked by maven in the test phase, could you check the version please.

Oh and other versions as well, such as maven or JUnit versions, it could help narrow the problem.




-- 
Bryce



--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


szczepiq

unread,
Jan 22, 2011, 5:25:57 PM1/22/11
to moc...@googlegroups.com
Hey,

Here's my bet: for some reason when you run in maven, the classes appear as from signed package. Since the package is signed, mockito cannot generate a new class in this package. So, what we do is we generate the class in a different package -> the consequence is the generated class cannot access mocked type -> BANG!

This is how we do it internally:

if (mockedType.getSigners() != null) {
  enhancer.setNamingPolicy ...

If my assumption is right it cannot be fixed on Mockito side. You've gotta publicize your classes! No problem with that -> privacy is evil anyway (go open languages!). :)

Cheers!
Szczepan

Dave

unread,
Jan 24, 2011, 11:04:44 AM1/24/11
to mockito

That appears to be the problem. I had upgraded to the Surefire 2.7
plugin
so I reverted back to 2.4 and the builds started working again.

I'm using JUnit 4.8.2.

Any idea if this is a solvable problem or is this something that
should be brought
up with the Surefire-plugin team?


On Jan 21, 4:28 pm, Brice Dutheil <brice.duth...@gmail.com> wrote:
> Hi Dave
>
> Eclipse bootstrasps the tests differently than maven. If the tests pass in
> Eclipse, there's something going wrong with the maven environement.
> I heard there was some problem with a recent update of surefire, which is
> invoked by maven in the test phase, could you check the version please.
>
> Oh and other versions as well, such as maven or JUnit versions, it could
> help narrow the problem.
>
> --
> Bryce
>
> > mockito+u...@googlegroups.com<mockito%2Bunsu...@googlegroups.com>
> > .

Dave

unread,
Jan 24, 2011, 11:05:11 AM1/24/11
to mockito
I'm not using signed jar files.

On Jan 22, 3:25 pm, szczepiq <szcze...@gmail.com> wrote:
> Hey,
>
> Here's my bet: for some reason when you run in maven, the classes appear as
> from signed package. Since the package is signed, mockito cannot generate a
> new class in this package. So, what we do is we generate the class in a
> different package -> the consequence is the generated class cannot access
> mocked type -> BANG!
>
> This is how we do it internally:
>
> if (mockedType.getSigners() != null) {
>   enhancer.setNamingPolicy ...
>
> If my assumption is right it cannot be fixed on Mockito side. You've gotta
> publicize your classes! No problem with that -> privacy is evil anyway (go
> open languages!). :)
>
> Cheers!
> Szczepan
>
> On Sat, Jan 22, 2011 at 12:28 AM, Brice Dutheil <brice.duth...@gmail.com>wrote:
>
> > Hi Dave
>
> > Eclipse bootstrasps the tests differently than maven. If the tests pass in
> > Eclipse, there's something going wrong with the maven environement.
> > I heard there was some problem with a recent update of surefire, which is
> > invoked by maven in the test phase, could you check the version please.
>
> > Oh and other versions as well, such as maven or JUnit versions, it could
> > help narrow the problem.
>
> > --
> > Bryce
>
> >> mockito+u...@googlegroups.com<mockito%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/mockito?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "mockito" group.
> > To post to this group, send email to moc...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mockito+u...@googlegroups.com<mockito%2Bunsu...@googlegroups.com>
> > .

Brice Dutheil

unread,
Jan 24, 2011, 12:31:57 PM1/24/11
to moc...@googlegroups.com
Hi Dave,

OK, it's a classloading issue with surefire 2.7. Anyway for your pleasure, the surefire seems to have fixed it in a 2.7.1 version.

http://maven.40175.n5.nabble.com/ANN-Maven-Surefire-Plugin-2-7-1-Released-td3318695.html


Regards,

-- 
Bryce


To unsubscribe from this group, send email to mockito+u...@googlegroups.com.

David Gageot

unread,
Jan 24, 2011, 12:34:33 PM1/24/11
to mockito
Hi Dave,

You should try Surefire 2.7.1
http://maven.40175.n5.nabble.com/ANN-Maven-Surefire-Plugin-2-7-1-Released-td3318695.html
Version 2.7 didn't work well with Mockito.

David.
Reply all
Reply to author
Forward
0 new messages