Home > OS >  TestNG classNot found with wrong name
TestNG classNot found with wrong name

Time:03-30

I am trying to run my TestNG test using my executable jar file which contains the test classes and testng.xml file via command line. I am using the following command

java -classpath C:\Users\user\.m2\repository\org\testng\testng\7.0.0\testng-7.0.0.jar;C:\Users\user\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;D:\MyProject\target\myjar.jar org.testng.TestNG -testjar D:\MyProject\target\myjar.jar -xmlpathinjar BOOT-INF/classes/testng.xml

It gives me the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/MyProject/common/ParticularClass (wrong name: BOOT-INF/classes/com/MyProject/common/ParticularClass)

I have generated my jar file with spring boot maven plugin and have verified that testng.xml, ParticularClass is present in the jar file.

CodePudding user response:

I was able to get TestNG working by creating separate jars for tests and dependencies using the following plugins:

          <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.myorg.myMainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.5.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <finalName>my-test-jar</finalName>
                    <archive>
                        <manifest>
                             <mainClass>com.myorg.myMainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The spring server was run using separate jar and test were run using test and dependency jar. Using the command

 java -classpath myjar-with-dependencies.jar;my-test-jar-tests.jar org.testng.TestNG -testjar my-test-jar-tests.jar -xmlpathinjar testng.xml
  • Related