Mockito Powermock Test run fails with ClassNotFoundException: org.mockito.cglib.proxy.MethodInterceptor - maven

I'm migrating a maven java8 project to Java 11. All the source files and test files compiles successfully. There are tests that makes use of mockito/powermock.
When I try to mvn clean install, the build fails because of the failures in tests. All the tests fails with the same cause (java.lang.ClassNotFoundException: org.mockito.cglib.proxy.MethodInterceptor) I'm using surefire plugin version 3.3.0-M3.
Please see relevant sections in pom.xml below. Please help me to resolve these ClassNotFoundException and make the build success.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>
--illegal-access=permit
</argLine>
</configuration>
</plugin>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
UPDATE:
The exception is coming from the APIs of following dependency which requires mockito 1.9.5.
<dependency>
<groupId>com.googlecode.catch-exception</groupId>
<artifactId>catch-exception</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>

Related

Jenkins + surefire plugin: Junit 5 tests are not executed in a multi test provider job

I've been struggling with this for a long time already... :(
I have a test project based on Junit4 for which I decided, innocently, to start using Junit5 for the next tests I write.
For some reason, the Jenkins job refuses to run the JUnit5 tests:
"No tests were executed!"
So, all in all, I got several JUnit4 based tests which execute, and several JUnit5 based tests which do not.
I tried to follow the guidelines here, but nothing worked for me and I got mixed up following it.
I'd appreciate your assistance with this problem.
My current relevant pom configuration is :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M5</version>
</dependency>
</dependencies>
</plugin>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
Maybe there are some conflicts here, I couldn't tell...

maven-surefire-plugin doesnot work in SpringBoot 2.2.2.RELEASE and above

I have used maven-surefire-plugin in my Maven project to execute the tests in parallel.
everything work great.
When I have upgraded to SpringBoot 2.2.2.RELEASE and above, tests stop running in parallel.
This is how I use the plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<parallel>methods</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
</configuration>
</plugin>
Is there a way to execute tests in parallel ? with this plug-in ?
I have uploaded a small maven project with two modules:
a not working module with springBoot 2.2.6.RELEASE
a working module with springBoot 1.5.9.RELEASE
Both modules are identical except SprintBoot version
Creating a configuration in pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.vintage</groupId>
</exclusion>
<exclusion>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
The problem here is of course that you are using JUnit 4 only.

Maven and JUnit 5: run a single test in a class

I have been trying to run unsuccessfully a single test in one class with Maven (version 3.3.9) and JUnit 5 (NOT 4) with the command:
mvn -Dtest=EmitRulesTest#cr_filter_contact_points_for_C4C_output test
This command executes all tests.
Trying out this command actually executes all tests in the class:
mvn test -Dtest=EmitRulesTest
This is my JUnit 5 Maven configuration:
<dependencies>
...
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.0.0-RC2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<systemPropertiesFile>${basedir}/src/test/resources/definitions/system.properties</systemPropertiesFile>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0-RC2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Further references: Running a Single Test using Maven
You can use this format:
mvn test -Dtest=TestClass#testMethod
You can find out more information here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
This is an excerpt of my POM.xml
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
...
<properties>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.1.0</junit.jupiter.version>
<junit.vintage.version>5.1.0</junit.vintage.version>
<junit.platform.version>1.1.0</junit.platform.version>
</properties>

Maven assembly plugin including multiple versions of Spring XSD files

We are packaging our Spring application into a jar using Maven. Initially, for testing, I was building a folder with all the dependency jars within Eclipse:
File -> Export -> Runnable Jar -> Copy required libraries into a sub folder
(This actually creates the application jar as well, but that I built outise Eclipse using Maven).
The application ran fine from the command line. Now, I have added the assembly plugin (jar-with-dependencies)to the pom.xml and am using that to generate one jar with all the classes from all the dependent jars. This will not run, and it appears the issue is that multiple versions of various Spring XSD files have been included in the dependency-jar.
I built a maven dependency tree, (mvn dependency:tree >dependencies.txt), but this does not help to show what is causing this problem,as it only shows dependencies between jars, not at the class or file level . These are the jars in our build:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.jdbc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-parent</artifactId>
<version>1.2.2.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-manager</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-resources</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.0.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>com.springsource.org.castor</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>com.springsource.net.sourceforge.jtds</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.jms</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jms</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
<version>$2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-xml</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jdbc</artifactId>
<version>${spring.integration.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>com.springsource.org.aspectj.tools</artifactId>
<version>1.6.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>com.springsource.org.aspectj.runtime</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>com.springsource.org.aspectj.weaver</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.solace</groupId>
<artifactId>sol-common</artifactId>
<version>6.0.0.146</version>
</dependency>
<dependency>
<groupId>com.solace</groupId>
<artifactId>sol-jms</artifactId>
<version>6.0.0.146</version>
</dependency>
<dependency>
<groupId>com.solace</groupId>
<artifactId>sol-jcsmp</artifactId>
<version>6.0.0.146</version>
</dependency>
Any help is very much appreciated.
Yes all the spring jars have spring.schemas and spring.handlers (which take care of the namespace handling) files in their respective META-INFs. Those files will get overwritten by each other. You will need to use the shade plugin (instead of assembly) with ResourceTransformers. The two combined, will merge the file content of the same file name onto one file. An example of the plugin for spring use would be something like this: (obtained from the ResourceTransformer link above). The two combined, will merge the file content of the same file name onto one file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I'd also recommend looking into Spring Boot. It makes running Spring apps a lot easier, without having to deal with this problem.
NOTE: And just an FYI, there are not multiples of the xsd files. Each spring jar has its own set with different name and versions. What you are duplicates are the spring.schemas and spring.handlers. They handle the namespace "directing" for each of the jars. But when you use the assembly plugin to build an uber jar, only one of the files will be used. So one of the many spring jars will be able to handle the namespaces
EDIT:
Sorry about my NOTE, I misread your title. The versions of the xsd don't make a difference. They're all there for backward compatability. The version you action use is either specified on the xml app context files, of if not specified there, the latest version will be used

How to use a maven package as both a dependency and a plugin

I am putting together a new project using Maven and JUnit. I am using the t7 plugin to run the application under tomcat after maven has compiled and run tests. I didn't have any problems until I wanted to set up the javaURLContextFactory in a JUnit test. The javaURLContextFactory is in the t7 plugin, but not in any of the dependencies for the project. With the plugin only defined as a plugin, the JUnit test fails, as it can't find the javaURLContextFactory class. If add the plugin as a dependency only, The JUnit test works, bu then it can't find the plugin when I want to run or debug. If I define it in both, I get bizarre errors related to parsing the web.xml.
Here is my current pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.star2star</groupId>
<artifactId>distribute</artifactId>
<packaging>war</packaging>
<version>0.8.1-SNAPSHOT</version>
<name>distribute Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>smtp</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0-rc1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-api</artifactId>
<version>7.0.19</version>
</dependency>
</dependencies>
<build>
<finalName>distribute-v1</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>com.googlecode.t7mp</groupId>
<artifactId>maven-t7-plugin</artifactId>
<version>0.9.10.M8</version>
</plugin>
</plugins>
</build>
</project>
Is there any way to reference the classes in the plugin as a dependency, or reference the dependency as a plugin? Is there some other method of fixing this?
Found a solution after talking to a co-worker.
By including the plugin as a dependency, I know it's bad, and setting the scope to provided, it was available for the JUnit test, but not included in the war when it was deployed using the t7 plugin. Then the unit tests were able to run, with the javaURLContextFactory and it still deployed properly.

Resources