首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于Maven安装的SpringBoot HttpRequestTest NoClassDefFound

用于Maven安装的SpringBoot HttpRequestTest NoClassDefFound
EN

Stack Overflow用户
提问于 2021-01-25 10:39:57
回答 1查看 100关注 0票数 0

当我试图使用SpringBoot创建一个Jar文件时,我会得到以下错误。

代码语言:javascript
复制
[ERROR]   HttpRequestTest.homePageReturnsVersionNumberCorrectly_thenSuccess:27 NoClassDefFound
[ERROR]   ProjectRepositoryIntegrationTest.ifNewProjectSaved_thenSuccess:41 NoClassDefFound
[INFO] 
[ERROR] Tests run: 3, Failures: 0, Errors: 2, Skipped: 0

项目结构:

代码语言:javascript
复制
/project-management
/project-management/src/main/java
com.jrp.pma
/project-management/src/main/java/com/jrp/pma/ProjectManagementApplication.java
com.jrp.pma.controllers
/project-management/src/main/java/com/jrp/pma/controllers/EmployeeController.java
/project-management/src/main/java/com/jrp/pma/controllers/HomeController.java
/project-management/src/main/java/com/jrp/pma/controllers/ProjectController.java
com.jrp.pma.dao
com.jrp.pma.dto
com.jrp.pma.entities
com.jrp.pma.services
com.jrp.pma.springExample
com.jrp.utils
/project-management/src/main/resources
/project-management/src/test/java
com.jrp.pma
com.jrp.pma.controllers
com.jrp.pma.dao
/project-management/src/test/java/com/jrp/pma/dao/ProjectRepositoryIntegrationTest.java
/project-management/src/test/resources
/project-management/src/test/resources/application.properties
/project-management/src/test/resources/data.sql
/project-management/src/test/resources/drop.sql
/project-management/src/test/resources/schema.sql
/project-management/target/generated-sources/annotations
/project-management/target/generated-test-sources/test-annotations
/project-management/src
/project-management/src/main
/project-management/src/test
/project-management/target
/project-management/HELP.md
/project-management/mvnw
/project-management/mvnw.cmd
/project-management/pom.xml

测试:

代码语言:javascript
复制
  package com.jrp.pma.dao;
    import static org.junit.Assert.assertEquals; 
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    //import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
    import org.springframework.boot.test.context.SpringBootTest;
    //import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.jdbc.Sql;
    import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
    import org.springframework.test.context.jdbc.SqlGroup;
    import org.springframework.test.context.junit4.SpringRunner;
    
    
    //import com.jrp.pma.dao.ProjectRepository;
    import com.jrp.pma.entities.Project;
    
    
    
    //@ContextConfiguration(classes=ProjectManagementApplication.class)
    
    //@DataJpaTest
    
    //@SqlGroup({@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts= {"classpath:schema.sql", "classpath:data.sql"})})
    
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    @SqlGroup({@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts= {"classpath:schema.sql", "classpath:data.sql"}),
        @Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts= "classpath:drop.sql")})
    public class ProjectRepositoryIntegrationTest {
        
        @Autowired
        ProjectRepository proRepo;
        
        @Test
        public void ifNewProjectSaved_thenSuccess() {;
            Project newProject = new Project("New Test Project", "COMPLETE", "Test Description");
            proRepo.save(newProject);
            
            assertEquals(5, proRepo.findAll().size());
    //      assertEquals(1, proRepo.findAll().size());
        }
    }

POM.XML文件

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jrp</groupId>
    <artifactId>project-management</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project-management</name>
    <description>project management application</description>

    <properties>
        <java.version>15</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
    
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制台

代码语言:javascript
复制
2021-01-25 05:35:15.946  INFO 20352 --- [           main] c.j.p.d.ProjectRepositoryIntegrationTest : Starting ProjectRepositoryIntegrationTest using Java 15.0.1 on DESKTOP-2M3J99U with PID 20352 (started by alex in C:\Users\alex\Documents\workspace-spring-tool-suite-4-4.9.0.RELEASE\project-management)
2021-01-25 05:35:15.946  INFO 20352 --- [           main] c.j.p.d.ProjectRepositoryIntegrationTest : No active profile set, falling back to default profiles: default
2021-01-25 05:35:16.048  INFO 20352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-01-25 05:35:16.061  INFO 20352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 12 ms. Found 2 JPA repository interfaces.
2021-01-25 05:35:16.101  INFO 20352 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$64b58f15] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-01-25 05:35:16.106  INFO 20352 --- [           main] .w.s.a.s.AnnotationActionEndpointMapping : Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
2021-01-25 05:35:16.141  INFO 20352 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Starting...
2021-01-25 05:35:16.143  INFO 20352 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Start completed.
2021-01-25 05:35:16.176  INFO 20352 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-01-25 05:35:16.181  INFO 20352 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate: drop table if exists employee CASCADE 
Hibernate: drop table if exists project CASCADE 
Hibernate: drop table if exists project_employee CASCADE 
Hibernate: drop sequence if exists project_seq
Hibernate: create sequence project_seq start with 1 increment by 1
Hibernate: create table employee (employee_id bigint not null, email varchar(255), first_name varchar(255), last_name varchar(255), primary key (employee_id))
Hibernate: create table project (project_id bigint not null, description varchar(255), name varchar(255), stage varchar(255), primary key (project_id))
Hibernate: create table project_employee (project_id bigint not null, employee_id bigint not null)
Hibernate: alter table project_employee add constraint FKn5yqs0xm3rmsg62n84ccyk4k0 foreign key (employee_id) references employee
Hibernate: alter table project_employee add constraint FK1907nkisp2dlsswuycpnakiv8 foreign key (project_id) references project
2021-01-25 05:35:16.213  INFO 20352 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-01-25 05:35:16.213  INFO 20352 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-01-25 05:35:16.342  WARN 20352 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-01-25 05:35:16.382  INFO 20352 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-25 05:35:16.394  INFO 20352 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:f148eb08-7d43-4f50-9b33-270a6bd3468b'
2021-01-25 05:35:16.571  INFO 20352 --- [           main] c.j.p.d.ProjectRepositoryIntegrationTest : Started ProjectRepositoryIntegrationTest in 0.64 seconds (JVM running for 5.169)
Hibernate: call next value for project_seq
Hibernate: insert into project (description, name, stage, project_id) values (?, ?, ?, ?)
Hibernate: select project0_.project_id as project_1_1_, project0_.description as descript2_1_, project0_.name as name3_1_, project0_.stage as stage4_1_ from project project0_
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.697 s <<< FAILURE! - in com.jrp.pma.dao.ProjectRepositoryIntegrationTest
[ERROR] ifNewProjectSaved_thenSuccess  Time elapsed: 0.058 s  <<< ERROR!
java.lang.NoClassDefFoundError: org/junit/Assert
    at com.jrp.pma.dao.ProjectRepositoryIntegrationTest.ifNewProjectSaved_thenSuccess(ProjectRepositoryIntegrationTest.java:41)
Caused by: java.lang.ClassNotFoundException: org.junit.Assert
    at com.jrp.pma.dao.ProjectRepositoryIntegrationTest.ifNewProjectSaved_thenSuccess(ProjectRepositoryIntegrationTest.java:41)

[INFO] Running com.jrp.pma.ProjectManagementApplicationTests
2021-01-25 05:35:16.637  INFO 20352 --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.jrp.pma.ProjectManagementApplicationTests], using SpringBootContextLoader
2021-01-25 05:35:16.638  INFO 20352 --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [com.jrp.pma.ProjectManagementApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
2021-01-25 05:35:16.638  INFO 20352 --- [           main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.jrp.pma.ProjectManagementApplicationTests]: ProjectManagementApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2021-01-25 05:35:16.641  INFO 20352 --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration com.jrp.pma.ProjectManagementApplication for test class com.jrp.pma.ProjectManagementApplicationTests
2021-01-25 05:35:16.641  INFO 20352 --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2021-01-25 05:35:16.641  INFO 20352 --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@35e0d91e, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3204146d, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@3299e315, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@7320bccc, org.springframework.test.context.support.DirtiesContextTestExecutionListener@403ba39f, org.springframework.test.context.transaction.TransactionalTestExecutionListener@520e6089, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@2ff2a096, org.springframework.test.context.event.EventPublishingTestExecutionListener@59c2bf78, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@44f8d884, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@65d3e2ba, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@5f9f1886, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@136a5572, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@11548363, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@6be865c1]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 s - in com.jrp.pma.ProjectManagementApplicationTests
2021-01-25 05:35:16.666  INFO 20352 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2021-01-25 05:35:16.667  INFO 20352 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-01-25 05:35:16.668  INFO 20352 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2021-01-25 05:35:16.668  INFO 20352 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
Hibernate: drop table if exists employee CASCADE 
Hibernate: drop table if exists project CASCADE 
Hibernate: drop table if exists project_employee CASCADE 
2021-01-25 05:35:16.668  INFO 20352 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
Hibernate: drop sequence if exists project_seq
2021-01-25 05:35:16.668  INFO 20352 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
Hibernate: drop table if exists employee CASCADE 
Hibernate: drop table if exists project CASCADE 
Hibernate: drop table if exists project_employee CASCADE 
Hibernate: drop sequence if exists project_seq
2021-01-25 05:35:16.670  INFO 20352 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown initiated...
2021-01-25 05:35:16.670  INFO 20352 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2021-01-25 05:35:16.672  INFO 20352 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2021-01-25 05:35:16.672  INFO 20352 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown completed.
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   HttpRequestTest.homePageReturnsVersionNumberCorrectly_thenSuccess:27 NoClassDefFound
[ERROR]   ProjectRepositoryIntegrationTest.ifNewProjectSaved_thenSuccess:41 NoClassDefFound
[INFO] 
[ERROR] Tests run: 3, Failures: 0, Errors: 2, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.994 s
[INFO] Finished at: 2021-01-25T05:35:17-05:00
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project project-management: There are test failures.
[ERROR] 
[ERROR] Please refer to C:\Users\alex\Documents\workspace-spring-tool-suite-4-4.9.0.RELEASE\project-management\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[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:
EN

回答 1

Stack Overflow用户

发布于 2021-01-25 10:46:16

您需要将JUnit作为依赖项。

代码语言:javascript
复制
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version> <!-- Or whatever JUnit you're using. -->
</dependency>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65883089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档