NoClassDefFoundError when attempting to run unit tests when building Jenkins plugin - maven

I have a custom Jenkins plugin that I've built. I have some test cases that leverage some code out of the jenkins-test-harness project (namely the Junit JenkinsRule.)
Anyway, the unit tests run and pass when I run them in IntelliJ. No exceptions, no errors.
When I try to run them from the Maven commandline, however, all of the tests that rely on the JenkinsRule fail:
[ERROR] testGetLastDate(com.mycompany.myplugin.portlet.utils.UtilsHudsonTest) Time elapsed: 0 s <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.jvnet.hudson.test.TestPluginManager
at org.jvnet.hudson.test.JenkinsRule.<init>(JenkinsRule.java:325)
at com.mycompany.myplugin.portlet.utils.UtilsHudsonTest.<init>(UtilsHudsonTest.java:19)
That TestPluginManager is in the jenkins-test-harness jar. The JenkinsRule that attempts to call it is right next to it in the same package in the same jar, so clearly the jar itself is successfully on the classpath.
I can't quite figure out why I can't run this job from the commandline using mvn test.
My POM is very simple -- I declare a Parent on the Jenkins plugin-3.2 pom:
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.2</version>
<relativePath/>
</parent>
And I have a dependency on the test harness:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>dashboard-view</artifactId>
<version>${dashboard-view.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-test-harness</artifactId>
<scope>test</scope>
<version>2.34</version>
</dependency>
</dependencies>
(That's the entire dependencies section, no omissions.)
And in my build section, I only declare the compiler, surefire, and hpi plugins:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>hpi</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
(The surefire config is inherited from the plugin-3.2 parent.)
Here's the bit of the UtilHudsonTest that causes the error -- line 19 is the #Rule annotation:
public class UtilsHudsonTest {
#Rule // This is line 19
public JenkinsRule j = new JenkinsRule();
#Test
public void testGetLastDate() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("prj1");
prj.scheduleBuild2(0).get();
FreeStyleProject prj2 = j.createFreeStyleProject("prj2");
prj2.scheduleBuild2(0).get();
List<Job> jobs = new ArrayList<>();
jobs.add(prj);
jobs.add(prj2);
LocalDate lastDate = Utils.getLastDate(jobs);
assertNotNull(lastDate);
}
// other tests
}
I'm kind of stumped why this would work in IntelliJ and not in Maven. A mvn dependency:tree shows that the correct and expected dependencies. Any ideas in how to get this plugin to successfully run its test from commandline?

Related

Junit test works only when renamed test class name

I am using junit4 to run test. My test class is as below in test->java->com.example->ProductIT.java
When I run mvn clean install on my project, below test is not recognized and not run.
However, when I rename the test class to ProductTest, it works.
I do not understand the difference. Why does it work when I rename ?
#ContextConfiguration(locations = {"classpath:META-INF/spring/test-config.xml"})
#RunWith(SpringJunit4ClassRunner.class)
public class ProductIT{
#Autowried
private ProductServiceImpl serviceImpl;
#Test
public void test() throws Exception{
assertNotNull(serviceImpl)
}
}
pom.xml
<dependencyManagement>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core></artifactId>
<version>4.2.4-RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure></artifactId>
<version>4.2.4-RELEASE</version>
</dependency>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch></artifactId>
<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter></artifactId>
<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test></artifactId>
<scope>test</scope>
<dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The *IT postfix (for integration test) is the default test identifier for the Maven Failsafe plugin. Spring Boot projects don't contain this plugin by default and you have to explicitly add it to your project.
The *Test postfix is the default test identifier for the Maven Surefire Plugin which is used as part of the test phase of the default lifecycle: mvn test.
This answer sheds more light on the differences between these plugins.
In case you want to split your tests (unit & integration) and run them separately (first unit, then integration), add the Maven Failsafe Plugin to your project:
<project>
<!-- other dependencies -->
<build>
<!-- further plugins -->
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
... and it should recognize your existing ProductIT when you run mvn verify (or mvn install).
For more information on such testing defaults and standards, consider this guide on testing Java applications with Maven.

Run a spring boot app located on another project before running karate tests? [duplicate]

This question already has an answer here:
Is there a way to run Karate tests as an integration test suite against a pre-booted spring boot server?
(1 answer)
Closed 2 years ago.
I'm trying to build some karate tests to a spring boot application. Here's the project structure :
co-training-backend (parent)
|_ co-training-rest (maven module)
|_ co-training-rest-karate (maven module)
If i run the server and launch the karate tests everything works fine. Now i'd like to automate that by running the server from the rest-karate module.
Here's my config :
class CoTrainingTests {
private static ConfigurableApplicationContext context = null;
#BeforeAll
public static void setUp() throws Exception {
context = SpringApplication.run(RestBootstrap.class, new String[]{});
}
#Karate.Test
Karate testAll() {
return Karate.run().relativeTo(getClass());
}
#AfterAll
public static void tearDown() throws Exception {
if(context!=null)
context.stop();
}
}
pom.xml
<artifactId>co-training-rest-karate</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit-jupiter.version>5.6.2</junit-jupiter.version>
<karate.version>0.9.6</karate.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>${karate.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>${karate.version}</version>
<scope>test</scope>
</dependency>
<!-- this is the spring boot rest project -->
<dependency>
<artifactId>co-training-rest</artifactId>
<groupId>com.co.training</groupId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
</build>
If i run the tests from the command line i have this maven compiler error :
mvn clean test
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile (default-testCompile) on project co-training-rest-karate: Compilation failure
[ERROR] /home/salto/tutorials/co-training/co-training-backend/co-training-rest-karate/src/test/java/cotraining/CoTrainingTests.java:[3,28] package com.co.training.rest does not exist
but this class exists in the rest dependency :
ls /home/salto/.m2/repository/com/co/training/co-training-rest/1.0.0-SNAPSHOT/co-training-rest-1.0.0-SNAPSHOT.jar
/home/salto/.m2/repository/com/co/training/co-training-rest/1.0.0-SNAPSHOT/co-training-rest-1.0.0-SNAPSHOT.jar
any idea ?
You can use the integration-test phase of the maven lifecycle:
pre-integration-test: start the co-training-rest module
integration-test: run you karate integration tests
post-integration-test: shut down the co-training-rest module and do
any other necessary cleanup

Junit test cases for multiple dependent project in POM.xml

I have a project Test-A, in this I have junit test class for the application.
Now, I have another project called Test-B, in this I have junit test class for the application.
Test-B, I have included project-A i.e modified POM.xml and included as:
POM.xml:
<dependency>
<groupId>com.abc.test-A</groupId>
<artifactId>TestA</artifactId>
<version>1.7</version>
<scope>test</scope>
</dependency>
question:
When I run mvn test for Test-B project, this has to run Test-A junit test files also. how can I run this kind of tests?
I am junit 4.9 version.
You will have to use the Maven Source Plugin to package the test source.
Here is an example for the build plugin to package the test source when you install or deploy the project:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>test-jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
You will have to add the dependency for the tests as a test-jar type:
<dependency>
<groupId>com.abc.test-A</groupId>
<artifactId>TestA</artifactId>
<version>1.7</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Now you have access to the tests from artifact TestA that you can run directly.
Running tests for your project B will not run tests for any dependency. For example your project would also have a dependency to junit. You don't expect those tests to run.

Maven throws error while running testng test cases

I have steup Eclipse + Maven + TestNG and I intend to run Selenium Test cases.
This is my POM File:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyGroupId</groupId>
<artifactId>TestSuite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.32.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Now when I try to run Maven test, I get following error:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
org.apache.maven.surefire.util.SurefireReflectionException:
java.lang.reflect.InvocationTargetException; nested exception is
java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:130)
at org.apache.maven.surefire.booter.SurefireReflector.instantiateProvider(SurefireReflector.java:247)
at org.apache.maven.surefire.booter.ProviderFactory.createProvider(ProviderFactory.java:81)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:171)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.NoSuchMethodError: org.apache.maven.surefire.providerapi.ProviderParameters.getRunOrderCalculator()Lorg/apache/maven/surefire/util/RunOrderCalculator;
at org.apache.maven.surefire.testng.TestNGProvider.<init>(TestNGProvider.java:67)
... 10 more
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Can someone suggest me what's that I am missing here.
Thanks in advance.
You have to define the maven-surefire-plugin in the pluginManagement section like the following:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
To use testng in relationship with the maven-surefire-plugin you usually only need to add testng as a dependency nothing more.
Furthermore remove the dependency you've given:
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.14.1</version>
</dependency>
Apart from the above this is looking more like an integration tests which is the job of maven-failsafe-plugin and not of maven-surefire-plugin.
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
This means your tests (presumably integration tests based on the selenium dependency) can be run by using:
mvn verify
Maybe will help somebody. Me helped - change version dependency of testng
It was:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
And has become:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
I also had an issue that resulted in this error, but for me it turned out to be missing file access rights for the account running the tests to the test folder.
Might be something to check out
changing the testNG dependency version in pom.xml from 6.14.3 to 6.10 will help out in avoiding the exception cases.
For me it got resolved after adding Surefire plugin in my Pom.XML as showed below:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
and you have to make sure that, testNG.xml must be created.
You need to give the exact file name of testng xml file inside "suiteXmlFile" tag of pom.xml.
As per my understanding, this error was generated because of TestNG not configured with the maven Surefire properly, surefire are used to run our Tests. Since I'm using TestNg it must be configured with surefire.

Can't get QueryDsl / APT to generate Q classes

I'm trying to use QueryDsl in a new Spring project. I'm new to QueryDsl, and pretty new to maven and Spring, so I may be missing something fairly basic, but I can't get QueryDsl / maven-apt-plugin to generate my Q classes. The Querydsl reference makes sound so easy; I think I did exactly what it said:
I configured pom.xml with:
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
and:
<repository>
<id>QUERYDSL</id>
<url>http://source.mysema.com/maven2/releases</url>
<layout>default</layout>
</repository>
and:
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>2.5.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
I have two #Entity's in that project.
mvn clean install does not result in any output to target/generated-sources/java/
What am I missing?
I tried mvn apt:process, and it results in:
[ERROR] Failed to execute goal com.mysema.maven:maven-apt-plugin:1.0.3:process (default-cli) on project logging-implementation: Either processor or processors need to be given -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.mysema.maven:maven-apt-plugin:1.0.3:process (default-cli) on project logging-implementation: Either processor or processors need to be given
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
Any suggestions?
Thanks!
OK, I got it.
I don't understand it (I'm a Maven noob), but here's what worked:
In the parent pom.xml, I have
<build>
<pluginManagement>
<plugins>
the maven-apt-plugin definition shown above
</plugin>
<pluginManagement>
</build>
and in the project's POM I have:
<build>
<plugins>
the **exact same** maven-apt-plugin definition shown above
</plugin>
</build>
without the <pluginManagement> level betweeen <build> and <plugins>, following the instructions at http://mojo.codehaus.org/apt-maven-plugin/plugin-info.html
You are calling the goal directly, but the configuration is execution specific. So either use apt via the standard maven lifecycle or make the configuration general.

Resources