问题
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