How to write JUnit test with Spring Autowire?

前端 未结 6 482
孤城傲影
孤城傲影 2020-12-03 06:45

Here are the files that I use:

component.xml




        
6条回答
  •  时光取名叫无心
    2020-12-03 07:33

    In Spring 2.1.5 at least, the XML file can be conveniently replaced by annotations. Piggy backing on @Sembrano's answer, I have this. "Look ma, no XML".

    It appears I to had list all the classes I need @Autowired in the @ComponentScan

    @RunWith(SpringJUnit4ClassRunner.class)
    @ComponentScan(
        basePackageClasses = {
                OwnerService.class
                })
    @EnableAutoConfiguration
    public class OwnerIntegrationTest {
        
        @Autowired
        OwnerService ownerService;
        
        @Test
        public void testOwnerService() {
           Assert.assertNotNull(ownerService);
        }
    }
    

提交回复
热议问题