Getting “At least one JPA metamodel must be present” with @WebMvcTest

前端 未结 5 1340
逝去的感伤
逝去的感伤 2021-02-05 06:02

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

@RunWith(SpringRunner.class)
@WebMvcTest(DemoController.clas         


        
5条回答
  •  耶瑟儿~
    2021-02-05 06:21

    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 {
    
    }
    

    And here the tests:

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

提交回复
热议问题