获取“必须至少存在一个JPA元模型”与@WebMvcTest [英] Getting "At least one JPA metamodel must be present" with @WebMvcTest

查看:363
本文介绍了获取“必须至少存在一个JPA元模型”与@WebMvcTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手,试图为 @Controller 做一些基本的集成测试。

I'm fairly new to Spring, trying to do some basic integration tests for a @Controller.

@RunWith(SpringRunner.class)
@WebMvcTest(DemoController.class)
public class DemoControllerIntegrationTests {
    @Autowired
    private MockMvc mvc;

    @MockBean
    private DemoService demoService;

    @Test
    public void index_shouldBeSuccessful() throws Exception {
        mvc.perform(get("/home").accept(MediaType.TEXT_HTML)).andExpect(status().isOk());
    }
}

但我得到了


java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

与发布此错误的大多数人不同,我不想使用JPA 。我是否尝试错误地使用 @WebMvcTest ?如何找到邀请JPA加入此派对的Spring魔法?

Unlike most people posting this error, I don't want to use JPA for this. Am I trying to use @WebMvcTest incorrectly? How can I track down the Spring magic that's inviting JPA to this party?

推荐答案

删除所有 @EnableJpaRepositories来自 SpringBootApplication 类的 @EntityScan 代替这样做:

Remove any @EnableJpaRepositories or @EntityScan from your SpringBootApplication class instead do this:

package com.tdk;

@SpringBootApplication
@Import({ApplicationConfig.class })
public class TdkApplication {

    public static void main(String[] args) {
        SpringApplication.run(TdkApplication.class, args);
    }
}

并将其放在单独的配置类中:

And put it in a separate config class:

package com.tdk.config;

@Configuration
@EnableJpaRepositories(basePackages = "com.tdk.repositories")
@EntityScan(basePackages = "com.tdk.domain")
@EnableTransactionManagement
public class ApplicationConfig {

}

这里是测试:

@RunWith(SpringRunner.class)
@WebAppConfiguration
@WebMvcTest
public class MockMvcTests {

}

这篇关于获取“必须至少存在一个JPA元模型”与@WebMvcTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆