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

必须至少存在一个JPA元模型

卫嘉佑
2023-03-14

在测试我的Controller类时,我遇到了这个异常

Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
    at org.springframework.util.Assert.notEmpty(Assert.java:450)
    at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.<init>(JpaMetamodelMappingContext.java:54)
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:88)
    at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:43)
    at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:141)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698)

我的控制器测试类如下所示

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = {SensorController.class}, secure = false)
public class SensorControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private SensorService sensorService;

    .....
}

我的引导类

@SpringBootApplication(scanBasePackages = "com.javadroider")
@RestController
@EntityScan("com.javadroider.notifier.commons.model")
@EnableJpaRepositories(basePackages = "com.javadroider.notifier")
public class NotifierApplication {

    @GetMapping("/")
    public String home(){
        return "Welcome to Notifier";
    }

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

我的依赖项如下所示

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <scope>runtime</scope>
        </dependency>
</dependencies>

IllegalArgumentException:必须至少存在一个JPA元模型没有解决我的问题

共有1个答案

朱渝
2023-03-14

@webmvctest注释不会自动为您配置任何存储库或JPA层bean,因为它主要关注的只是控制器级别的测试

您的主类NotifierApplication尝试做得太多了。关于良好的编程实践,请参见单一责任原则:https://en.wikipedia.org/wiki/single_responsibility_principle

main类应该只用来定义应用程序它本身不应该是@RestController。例如,创建一个单独的类NotifierController,并将其作为@RestController,其中定义了endpoint:

@RestController 
public class NotifierController {

    @GetMapping("/")
    public String home(){
        return "Welcome to Notifier";
    }

}

并将其与主类分开:

@SpringBootApplication(scanBasePackages = "com.javadroider")
@EntityScan("com.javadroider.notifier.commons.model")
@EnableJpaRepositories(basePackages = "com.javadroider.notifier")
public class NotifierApplication {

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

它失败的原因是由于主类上的注释,WebMvcTest试图扫描JPA存储库和实体,但这没有为WebMVCTests配置。

 类似资料: