java - 在 Maven/Spring Boot 项目中运行测试时出现 MojoFailureException (NoClassDefFounderror)

标签 java spring maven spring-boot

尝试在 Maven/Springboot 项目中运行(集成)测试时出现错误。 我得到一个 initializationError >> NoClassDefFound在我要初始化的对象上。

错误日志:

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

我的 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>

我的测试类:

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

    @Autowired
    private SessionService sessionService;

    @Test
    public void testGetSessions() {}
}

文件夹结构:

  • 来源
    • 主要
      • java
        • IP2
          • 类/服务/ Controller
    • 测试
      • java
        • IP2
          • 测试类
      • 资源
        • IP2
          • testcontext.xml

我的服务使用@Service 注释进行注释。在testcontext.xml我有<context:component-scan base-package="ip2"/>

不知道如何解决这个错误。任何帮助将不胜感激!

最佳答案

终于自己找到了答案。

首先,我没能找到问题的根源,所以标题现在是相当具有误导性。问题是 Maven-failsafe-plugin 没有找到 spring boot 类。解决方案是降级到 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

关于java - 在 Maven/Spring Boot 项目中运行测试时出现 MojoFailureException (NoClassDefFounderror),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42444011/

相关文章:

java - 使用Object类实现数学运算的多态性

java - 使用 maven-archetype-webapp 创建的项目未找到 servlet

java - maven java.lang.IllegalAccessError : tried to access method com. google.common.collect.MapMaker.makeComputingMap

maven - 为什么我不能从本地存储库部署到远程 Maven 存储库?

java - 在jsp中接收类名作为用户输入有什么问题

java - 在java中协调保存在数据库和磁盘中

java - Spring Security抛出未经授权的异常而不是重定向到登录

spring - 使用 Ajax 使用 Spring security 登录

java - 为什么 Spring 注解和 xml 配置注入(inject)集合的方式不同?

java - 已弃用的认可标准覆盖机制和扩展机制的替代方案