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

前端 未结 5 1360
逝去的感伤
逝去的感伤 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:06

    Alternatively, you can define a custom configuration class inside your test case, including only the controller (plus its dependencies), to force Spring to use this context.
    Please note, you'll still have access to MockMvc and other goodness in your test case, if it's WebMvcTest annotated.

    @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());
        }
    
        @Configuration
        @ComponentScan(basePackageClasses = { DemoController.class })
        public static class TestConf {}
    

提交回复
热议问题