I have a Java Project in which I am writing a simple JUNIT test case. I have copied the applicatinoContext.xml file into the root java source directory. I've tried it with some of the recommended settings I have read of here on StackOverflow but still get the same error. Is this error happening due to my project being a java project and NOT a web project, or does that even matter? I'm not sure where im going wrong.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"C:/projs/sortation/src/main/java/applicationContext.xml"})
// Also tried these settings but they also didnt work,
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})
//@ContextConfiguration("classpath:applicationContext.xml")
@Transactional
public class TestSS {
@Autowired
private EmsDao dao;
@Test
public void getSites() {
List<String> batchid = dao.getList();
for (String s : batchid) {
System.out.println(s);
}
}
}
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")
From the Spring-Documentation: 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.
If you work with the absolute file path you have to use 'file:C:...' (if I understand the documentation correctly).
I had the same problem, and I was using the following plugin for tests:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<useFile>true</useFile>
<includes>
<include>**/*Tests.java</include>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
<junitArtifactName>junit:junit</junitArtifactName>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
The test were running fine in the IDE (eclipse sts), but failed when using command mvn test.
After a lot of trial and error, I figured the solution was to remove parallel testing, the following two lines from the plugin configuration above:
<parallel>methods</parallel>
<threadCount>10</threadCount>
Hope that this helps someone out!
I've faced this issue because during bootstrapping my spring project
using the class that implements ApplicationListener<ContextRefreshedEvent>
and inside onApplicationEvent
function it throws an exception
so make sure that your application bootstrap points do not throw any exception
in my case, I was using maven surefire plugin for testing so to debug the test process use this command
mvn -Dmaven.surefire.debug test