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

时间:2017-02-24 16:47:13

标签: 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() {}
}

文件夹结构:

  • SRC
      • 的java
        • IP2
          • 类/服务/控制器
    • 测试
      • 的java
        • IP2
          • testclasses
      • 资源
        • IP2
          • testcontext.xml

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

不知道如何解决此错误。任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:2)

终于找到了答案。

首先,我没有找到问题的根源,因此标题现在非常具有误导性。问题是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