JUnit Error: “Failed to load ApplicationContext”

喜欢而已 提交于 2020-01-24 20:14:22

问题


Nothing seems to work

I'm trying to run simple test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml"})
@Transactional
public class EmailServiceTest {

I've also tried:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"src/main/webapp/WEB-INF/applicationContext.xml"})
@Transactional
public class EmailServiceTest {

Along with a few different things in place of "location," such as "classpath" and "file."

The "ApplicationContext" is located:

src
    main
        webapp
            WEB-INF
                applicationContext.xml

But JUnit test still says: Failed to load ApplicationContext


回答1:


The issue was solved after we realized our build file was missing information pertaining to testing. Information such as the "app.properties" and " applicationContext" were not being copied into the testing resources. So technically, none of these were on the classpath.




回答2:


you need to use @ContextConfiguration(locations = { "file:./src/main/resources/ApplicationContext.xml" }) after @RunWith(SpringJUnit4ClassRunner.class). and test.java file create at /src/test/java/your-package-name.




回答3:


You have to specify an application context location on the classpath. src/main is added to the classpath. Therefore, you need to specify only webapp/WEB-INF/applicationContext.xml as follows: @ContextConfiguration(locations = {"webapp/WEB-INF/applicationContext.xml"}).



来源:https://stackoverflow.com/questions/44764218/junit-error-failed-to-load-applicationcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!