当前位置: 首页 > 知识库问答 >
问题:

在测试期间找不到所需的bean/没有名为'entityManagerFactory'的bean可用

齐航
2023-03-14

我正在开发一个小应用程序,只是为了自己尝试Spring Boot。它有五个不同的模块(eclipse项目):

模型——包含实体类、DTO和映射器以在它们之间切换

数据--包含存储库

服务——包含服务及其实现

reserve management--包含预订控制器和spring应用程序本身

人员管理-包含人员数据的控制器和Spring应用程序本身

如果我启动这两个应用中的任何一个,它们都可以正常运行,但在为它们开发测试时,我总是会遇到以下例外:

它还告诉我应该在配置中添加ReservesRepository类型的bean

为了简洁起见,我从类中删除了方法

测试类和pom。储量管理项目中的xml:


    package com.hotel.reservemanagement.service;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    import java.util.Optional;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.hotel.model.reserve.Reserve;
    import com.hotel.service.reserve.ReservesServiceImpl;
    
    @SpringBootTest(classes = ReservesServiceImpl.class)
    @ComponentScan("com.hotel.data")
    public class ReservesServiceTests {
        
        @Autowired
        private ReservesServiceImpl reservesService;
        
        @Test
        @Transactional
        void testSave () {
            Optional<Reserve> reserve = this.reservesService.findById(2L);
            reserve.get().setAdults(3L);
            this.reservesService.save(reserve.get());
            Optional<Reserve> reserveResult = this.reservesService.findById(2L);
            
            assertEquals(3L, reserveResult.get().getAdults());
        }
    }   


    <?xml version="1.0" encoding="UTF-8"?>
        <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.7.1</version>
                <relativePath/> <!-- lookup parent from repository -->
            </parent>
            <groupId>com.hotel</groupId>
            <artifactId>reserve-management</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <name>reserve-management</name>
            <description>Hotel reserves management</description>
            <properties>
                <java.version>11</java.version>
            </properties>
            <dependencies>
            
                <dependency>
                    <groupId>com.oracle.database.jdbc</groupId>
                    <artifactId>ojdbc8</artifactId>
                    <scope>runtime</scope>
                </dependency>
        
                <dependency> 
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-test</artifactId>
                    <scope>test</scope>
                </dependency>
                
                <dependency>
                    <groupId>com.hotel</groupId>
                    <artifactId>model</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                </dependency>
                
                <dependency>
                    <groupId>com.hotel</groupId>
                    <artifactId>service</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                </dependency>
            </dependencies>
        
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        
        </project>

储备服务及其实施和pom。xml in-service项目:


    package com.hotel.service.reserve;
    
    public interface ReservesService {
        //some code here    
    }


    package com.hotel.service.reserve;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.hotel.data.reserve.ReservesRepository;
    
    
    @Service
    public class ReservesServiceImpl implements ReservesService{
        
        @Autowired
        private ReservesRepository reserveRepo;
        //some code here
    }


    <?xml version="1.0" encoding="UTF-8"?>
    <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.7.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.hotel</groupId>
        <artifactId>service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>service</name>
        <description>Services for Hotel application</description>
        <properties>
            <java.version>11</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-web-services</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.hotel</groupId>
                <artifactId>data</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

数据项目中的存储库类和pom.xml:


    package com.hotel.data.reserve;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    import com.hotel.model.reserve.Reserve;
    
    @Repository
    public interface ReservesRepository extends JpaRepository<Reserve, Long> {
        //some code here
    }


    <?xml version="1.0" encoding="UTF-8"?>
    <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.7.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.hotel</groupId>
        <artifactId>data</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>data</name>
        <description>Data for Hotel application</description>
        <properties>
            <java.version>11</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>com.hotel</groupId>
                <artifactId>model</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            
        </dependencies>
    
        <build>
            <plugins>
            </plugins>
        </build>
    
    </project>

共有1个答案

伍溪叠
2023-03-14

我刚刚修改了测试类并添加了EntityScan(basePackages=“com.hotel”),现在这个测试确实有效了。但是,当从人员管理项目执行其他测试时:

    package com.hotel.personmanagement.controller;
    
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.junit.jupiter.api.BeforeAll;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.Mockito;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.junit.jupiter.SpringExtension;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.hotel.model.person.Person;
    import com.hotel.service.person.PersonsService;
    
    @ExtendWith(SpringExtension.class)  
    @WebMvcTest(PersonsController.class) 
    @AutoConfigureMockMvc
    @ComponentScan("com.hotel")
    @EntityScan(basePackages = "com.hotel")
    public class PersonsControllerTest {
    
        @MockBean
        private PersonsService personsService;
        
        @Autowired
        private MockMvc mockMvc;
        
        @Autowired
        private ObjectMapper objectMapper;
        
        private static Person person;
        
        @BeforeAll
        public static void init () {
            person = new Person("Carlos", "123454321N", "Calle x");
        }
        
        @Test
        void getPersonsTest () throws Exception {
            List<Person> persons = Arrays.asList(person);
            
            Mockito.when(this.personsService.findAll()).thenReturn(persons);
            
            final String url = "/persons";
    
            final String expectedJson = objectMapper.writeValueAsString(persons);
            
            this.mockMvc.perform(MockMvcRequestBuilders
                    .get(url)
                    .accept(MediaType.APPLICATION_JSON))
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(status().isOk())
                    .andExpect(content().json(expectedJson));
                    
        }
        
    }

我得到了另一个错误:

原因:org.springframework.beans.factory.NoSuchBean定义异常:没有名为'entityManagerFactory'的bean可用

完整堆栈跟踪:

 类似资料:
  • 我被这个问题缠住了。当我尝试运行我的Spring启动应用程序时,我会收到以下消息: 我正在使用Spring Data JPA,如果我理解正确的话,当我使用例如JpaRepository接口时,entityManager会自动实现到我的项目中。我是否遗漏了什么?即使我使用的是JpaRepository,我是否应该为entityManager定义一个bean?我尝试将其更改为CrudRepositor

  • 我创建了一个Spring Boot应用程序,并使用我想要执行的方法创建了一个类。将项目部署为war文件时,我从stacktrac中获取错误。我想从类TennisExecitor中运行该方法。 没有名为'entityManagerFactory'的bean可用 创建名为'(内部bean)#366583f9'的bean时出错:设置构造函数参数时无法解析对bean'entityManagerFactor

  • 我正在尝试开发一个新的批与Spring批和jpa。当我尝试启动它时,我收到错误"嵌套异常是org.springframework.beans.factory.NoSuchBean定义异常:没有名为'entityManagerFactory'可用的bean" 我的pom.xml 我的数据源配置类: 我的存储库类: 我的实体类: pplication.properties: 如果我尝试启动应用程序,我

  • 我正在开发一个新的应用程序(对我来说),我在启动时收到了无bean“entityManagerFactory”可用错误。其他经历过这种情况的人表示,如果我正确定义了数据源,这种情况就不会发生。让我困惑的是,Spring Boot/JPA或Flyway(我也在使用)坚持将特定的属性名称用于您的一个数据源。我不知道如果你有一个以上的人会怎么处理。我需要更改我们的申请。属性文件,以便从环境变量中获取值,

  • 使用spring boot 2.0版本。0.M4我有这个问题:

  • 请求处理失败;嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:没有名为“transactionManager”的bean可用:没有为限定符“transactionManager”找到匹配的transactionManager bean-既没有限定符匹配,也没有bean名称匹配! 客户请求。JAVA 客户控制器 顾客道 spri