SecurityException when starting a simple JUnit + Mockito in an Eclipse RCP project

I have an Eclipse RCP project with several plugins. I am writing simple JUnit tests (without Eclipse / UI dependencies) as separate fragments for the plugin under the test.

When using Mockito and trying to mock the interface from another plugin (which is correctly exported, I can use the interface in my code), I get a SecurityException related to the class signature:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface ch.sbb.polar.client.communication.inf.service.IUserService
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 org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)

[...]

Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)

[...]

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

[...]

Caused by: java.lang.SecurityException: Signers of 'ch.sbb.polar.client.communication.inf.service.IUserService$$EnhancerByMockitoWithCGLIB$$a8bfe723' do not match signers of other classes in package

at java.lang.ClassLoader.checkPackageSigners(ClassLoader.java:361)

at java.lang.ClassLoader.defineClass(ClassLoader.java:295)

... 40 more

When I run tests as "JUnit Plugin tests", that is, in the OSGi environment, everything works as expected. But I would like to use normal JUnit execution because of speed; in the tested class I do not need the OSGi environment.

Does anyone know a way to do this?

+4
1

, , Eclipse Orbit Mockito ( ) , - CGLIB / Mockito.

. https://code.google.com/p/mockito/issues/detail?id=393. ​​ CGLIB head, . Mockito , Mockito, () , .

: Mockito

, Mockito JAR ( ) API.

Maven Tycho, JUnit, Hamcrest Mockito , , /classpath/classloader , :

  • Maven pom.xml:

    <packaging>eclipse-plugin</packaging>
    

    [...]

    <dependencies>
      <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.10.19</version>
      </dependency>
    </dependencies>
    

    [...]

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-test-libs</id>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <outputDirectory>lib</outputDirectory>
            <stripVersion>true</stripVersion>
            <includeScope>runtime</includeScope>
          </configuration>
        </execution>
      </executions>
    </plugin>
    
  • MANIFEST.MF:

    Bundle-ClassPath: lib/mockito-core.jar,
     lib/objenesis.jar
    Export-Package: org.mockito,
     org.mockito.runners
    Require-Bundle: org.junit;bundle-version="4.11.0";visibility:=reexport,
     org.hamcrest.library;bundle-version="1.3.0";visibility:=reexport,
     org.hamcrest.core;bundle-version="1.3.0";visibility:=reexport
    
  • , , unit test .

+4

All Articles