gpt4 book ai didi

java - 使用@WebMvcTest 获取 "At least one JPA metamodel must be present"

转载 作者:IT老高 更新时间:2023-10-28 20:52:45 27 4
gpt4 key购买 nike

我对 Spring 还很陌生,正在尝试为 @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 ApplicationContextCaused 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 魔法?

最佳答案

从您的 SpringBootApplication 类中删除任何 @EnableJpaRepositories@EntityScan 改为执行以下操作:

package com.tdk;

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

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

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

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 {

}

关于java - 使用@WebMvcTest 获取 "At least one JPA metamodel must be present",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41250177/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com