由于注入持久性依赖项失败,无法在 Spring Boot 应用程序上运行测试

Posted

技术标签:

【中文标题】由于注入持久性依赖项失败,无法在 Spring Boot 应用程序上运行测试【英文标题】:Can't run test on spring boot application because of injection of persistence dependencies failed 【发布时间】:2019-07-11 20:33:13 【问题描述】:

我有我的 java spring boot 应用程序,我想使用 mockmvc 编写一些测试;所以这是测试类:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = IndexController.class)
@ComponentScan(basePackages="com.sherlock.discoteque")
@EnableJpaRepositories("com.sherlock.discoteque.repository")
@EntityScan(basePackages="com.sherlock.discoteque")
public class DiscotequeApplicationTests 

private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setup() 
    this.mockMvc = MockMvcBuilders.standaloneSetup(webApplicationContext).build();


@Test
public void testAlbumInfo() throws Exception
   this.mockMvc.perform(get("/")).andExpect(status().isOk());

但是当我执行代码时出现以下错误消息:

com.sherlock.discoteque.controller.AlbumController 中的字段 albumRepository 需要一个无法找到的“javax.persistence.EntityManagerFactory”类型的 bean。

注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)

行动:

考虑定义一个 bean 类型 'javax.persistence.EntityManagerFactory' 在您的配置中。 ... 引起:org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“albumController”的 bean 时出错:不满意 通过字段“albumRepository”表达的依赖关系;嵌套异常 是 org.springframework.beans.factory.BeanCreationException: 错误 创建名为“albumRepositoryImpl”的bean:注入 持久性依赖失败;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 'javax.persistence.EntityManagerFactory' 类型的限定 bean 可用的 引起:org.springframework.beans.factory.BeanCreationException: 错误创建名为“albumRepositoryImpl”的 bean:注入 持久性依赖失败; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 'javax.persistence.EntityManagerFactory' 类型的限定 bean 可用

这很奇怪,考虑到没有测试类,一切都可以正常工作。这是类 AlbumRepositoryImpl

public class AlbumRepositoryImpl implements AlbumRepositoryCustom 

private final static String RECENT_ALBUMS_QUERY = "select * from album order by createdate desc limit ?";

@PersistenceContext
public EntityManager entityManager;

@Override
public List<Album> getRecentAlbums(int size) 
    if(size<1)
        throw new IllegalArgumentException();
    
    Query query = entityManager.createNativeQuery(RECENT_ALBUMS_QUERY, Album.class);
    query.setParameter(1, size);
    return query.getResultList();


在 AlbumController 内部我确实有该属性

   @Autowired
private AlbumRepository albumRepository;

我也有 AlbumRepository 接口(从 JpaRepository 扩展)。我真的不知道该怎么做才能使 Web 应用程序在测试中运行,有人可以帮助我吗?

【问题讨论】:

【参考方案1】:

在示例代码中,您正在尝试自动装配上下文,但您尚未提供测试配置。

在您的项目中,您已经定义了 JPA 实体管理器配置,但在测试文件中您没有提供该信息。除非您没有在测试类中提供必要的配置,否则 Spring 将无法启动容器。

你可以从https://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-configuration/那里得到一个想法

【讨论】:

【参考方案2】:

尝试使用注释设置spring boot profile - @ActiveProfiles("you_profile")

【讨论】:

以上是关于由于注入持久性依赖项失败,无法在 Spring Boot 应用程序上运行测试的主要内容,如果未能解决你的问题,请参考以下文章

注入自动装配的依赖项失败,无法自动装配字段

beans.factory.BeanCreationException:创建名为“dao”的bean时出错:注入持久性依赖项失败

Spring data jpa-未定义名为“entityManagerFactory”的bean;注入自动装配的依赖项失败

如何使用 Spring 将依赖项注入 HttpSessionListener?

使用限定符注入自动装配的依赖项失败

spring+hibernate:自动装配依赖注入失败