Testing with Spring and Maven: applicationContext

前端 未结 9 1574
-上瘾入骨i
-上瘾入骨i 2020-12-13 04:04

Seems that question old as world, but I still can\'t find out the solution..

I\'m trying to run simple test:

@RunWith(SpringJUnit4ClassRunner.class)
         


        
9条回答
  •  不知归路
    2020-12-13 04:41

    I think best practice is to put your application context file for testing PersonsPopulateTest-context.xml under src/test/resources. This file will be copied into target/test-classes and you can refer to it in your test class as below:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:**/PersonsPopulateTest-context.xml"})
    @Transactional
    public class PersonsPopulateTest {
    
    }
    

    If you still want to refer to applicationContext.xml under src/main/resources, then you have to include it as follow:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"file:src/main/resources/applicationContext.xml"})
    @Transactional
    public class PersonsPopulateTest {
    
    }
    

    It worked for me.

提交回复
热议问题