【问题标题】:Can't run test on spring boot application because of injection of persistence dependencies failed由于注入持久性依赖项失败,无法在 Spring Boot 应用程序上运行测试
【发布时间】: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 应用程序在测试中运行,有人可以帮助我吗?

【问题讨论】:

    标签: java unit-testing spring-boot junit spring-data-jpa


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-26
        • 2017-06-01
        • 2023-03-24
        • 2019-05-02
        • 2020-12-12
        • 1970-01-01
        • 2015-02-28
        • 2018-12-10
        相关资源
        最近更新 更多