简体   繁体   中英

Class Not Found Exception When Running JUnit Tests from test jar

I am building a project in a Windows Eclipse environment and then trying to test the project jar using the test jar in a Unix environment . When I run I get what is shown below. I am using Maven with the assembly plugin to package the project with the Test Jar and I can see the class that is supposedly missing inside the jar when I run the -tf command.

java -cp MyServerTest.jar:junit.jar junit.runner.JunitCore com.MyServer.services.ZoweAuthServiceTest

Results in:

java.lang.NoClassDefFoundError: com.MyServer.services.ZoweAuthService at com.MyServer.services.ZoweAuthServiceTest.testQueryToZowe(ZoweAuthServ iceTest.java:40) Caused by: java.lang.ClassNotFoundException: com.MyServer.services.ZoweAuthService

You could use the main answer from this post:

How to run JUnit test cases from the command line

Make sure to running from the current directory of your jar and adjust it accordingly to junit version.

I figured it out. I guess I should have mentioned I am using spring-boot. Spring places the main classes of my project under a special prefix in the jar file "BOOT-INF" which prevents the test class from finding the class it wants to test. I added the classifer exec to my pom. This creates two separate jars. One for executing and one that has the normal class references for testing.

`<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <classifier>exec</classifier>
    </configuration>
</plugin>` 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM