Home > database >  Is ApplicationContext file necessary if I want to run the unit test?
Is ApplicationContext file necessary if I want to run the unit test?

Time:10-29

everyone,

I'm new on web development. Recently, I start a tutorial to build Registration and Login with Spring Boot and MySQL Database. When I run the unit test, there are errors showing up. I cannot find ApplicationContext file in the tutorial, and also try serval methods on stack overflow e.g. change plug setting in pom.xml but isn't working.

Can anyone answer this problem?

Errors on IDE

CodePudding user response:

The application context is not a file – it's a term Spring uses to describe the entire state of your application. So the error means that Spring is not able to start your application because of missing database configuration.

CodePudding user response:

Looks like you are using maven (src/main/java). In this case put the applicationContext.xml file in the src/main/resources directory. It will be copied in the classpath directory and you should be able to access it with

@ContextConfiguration("/applicationContext.xml")

A plain path, for example, "context.xml", will be treated as a classpath resource from the same package in which the test class is defined. A path starting with a slash is treated as a fully qualified classpath location, for example "/org/example/config.xml". So, it's important that you add the slash when referencing the file in the root directory of the classpath.

  • Related