简体   繁体   English

MovenFailureException(NoClassDefFound)在Maven / Springboot项目中运行测试时

[英]MojoFailureException (NoClassDefFound) when running test in Maven/Springboot project

I'm getting an error when trying to run (integration) tests in Maven/Springboot project. 我在Maven / Springboot项目中尝试运行(集成)测试时遇到错误。 I'm getting an initializationError >> NoClassDefFound on the object I'm trying to initialize. 我正在尝试初始化的对象上获得一个initializationError >> NoClassDefFound

Error log: 错误日志:

Running ip2.TestSessionsIT
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.002 sec <<< FAILURE! - in ip2.TestSessionsIT
initializationError(ip2.TestSessionsIT)  Time elapsed: 0.002 sec  <<< ERROR!
java.lang.NoClassDefFoundError: Lip2/services/interfaces/SessionService;
Caused by: java.lang.ClassNotFoundException: ip2.services.interfaces.SessionService

17:20:39.089 [Thread-0] INFO org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@35a50a4c: startup date [Fri Feb 24 17:20:33 CET 2017]; root of context hierarchy
17:20:39.090 [Thread-0] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
17:20:39.090 [Thread-0] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@525f1e4e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,org.springframework.boot.test.mock.mockito.MockitoPostProcessor$SpyPostProcessor,org.springframework.boot.test.mock.mockito.MockitoPostProcessor]; root of factory hierarchy

Results :

Tests in error: 
  TestSessionsIT.initializationError » NoClassDefFound Lip2/services/interfaces/...

Tests run: 3, Failures: 0, Errors: 1, Skipped: 0

[INFO] 
[INFO] --- maven-failsafe-plugin:2.19.1:integration-test (integration-test) @ revCan ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.1.RELEASE:stop (post-integration-test) @ revCan ---
[INFO] Stopping application...
2017-02-24 17:20:39.518  INFO 23721 --- [           main] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.
2017-02-24 17:20:39.518  INFO 23721 --- [           main] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6a156a6a: startup date [Fri Feb 24 17:20:23 CET 2017]; root of context hierarchy
2017-02-24 17:20:39.521  INFO 23721 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2017-02-24 17:20:39.524  INFO 23721 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-02-24 17:20:39.529  INFO 23721 --- [           main] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:2, serverValue:69518}] to ds153689.mlab.com:53689 because the pool has been closed.
[INFO] 
[INFO] --- maven-failsafe-plugin:2.19.1:verify (default) @ revCan ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 42.439 s
[INFO] Finished at: 2017-02-24T17:20:39+01:00
[INFO] Final Memory: 47M/344M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.19.1:verify (default) on project revCan: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/robbevanwinckel/Desktop/team5years/team5yearswebsite/target/failsafe-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

My Pom.xml: 我的Pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.6.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals><goal>integration-test</goal></goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <phase>verify</phase>
                    <goals><goal>verify</goal></goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals><goal>start</goal></goals>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals><goal>stop</goal></goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

My test class: 我的考试班:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("testcontext.xml")
public class TestSessionsIT {

    @Autowired
    private SessionService sessionService;

    @Test
    public void testGetSessions() {}
}

Folder structure: 文件夹结构:

  • src SRC
    • main 主要
      • java java的
        • ip2 IP2
          • classes/services/controllers 班/服务/控制器
    • test 测试
      • java java的
        • ip2 IP2
          • testclasses testclasses
      • resources 资源
        • ip2 IP2
          • testcontext.xml testcontext.xml

My service is annotated with the @Service annotation. 我的服务使用@Service注释进行注释。 In the testcontext.xml I have <context:component-scan base-package="ip2"/> testcontext.xml我有<context:component-scan base-package="ip2"/>

No idea how to solve this error. 不知道如何解决这个错误。 Any help would be very appreciated! 任何帮助将非常感谢!

Finally found the answer myself. 终于找到了自己的答案。

First of all, I failed to find the root of the problem, so the title is now quite misleading. 首先,我没有找到问题的根源,因此标题现在非常具有误导性。 The problem was the the Maven-failsafe-plugin didn't find the spring boot class. 问题是Maven-failsafe-plugin没有找到spring boot类。 The solution is to downgrade to version 2.18.1. 解决方案是降级到2.18.1版。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.18.1</version>
</plugin>

https://github.com/spring-projects/spring-boot/issues/6254 https://github.com/spring-projects/spring-boot/issues/6254

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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