Issue
I'm trying to do a unit test in my application to test a a method on service layer.
I have the following classes.
AbstractTest.java
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.equadis.webapp.controller.UserController;
//inform spring which class to use when executing unit test
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=UserController.class)
public abstract class AbstractTest {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
}
and
UserServiceTest.java that extends AbstractTest
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.equadis.webapp.DAO.UserDAO;
import com.equadis.webapp.entity.UserVO;
import com.equadis.webapp.test.AbstractTest;
@Transactional
public class UserServiceTest extends AbstractTest {
@Autowired
private UserService service;
@Before
public void setup(){
service = new UserService();
}
@After
public void tearDown(){
//clean up after each test method
}
@Test
public void getListByID() {
UserVO user = service.getListByID(1);
System.out.println(user);
Assert.assertNotNull("failure - expected not null", user);
Assert.assertEquals("failure - expected id", 1, user.getId());
}
}
Noting that in Abstract.java class
@SpringApplicationConfiguration(classes = Application.class)
was replaced by
@SpringBootTest(classes=UserController.class)
So when i run a unit test i get the following errors:
Stack trace:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:201)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: Can not build an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
at org.springframework.util.Assert.notNull(Assert.java:115)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:169)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:197)
... 17 more
Any help concerning this?
Solution
thanks everyone for your help , after reading all your comments and doing some research i've figured out the solution , with adding the following annotation:
@ContextConfiguration(locations={"/myxmlfile.xml"})
that contain the confiuration.
Answered By - Elias