I am working on a spring 3 web service application and on this I am trying to load ApplicationContext from my servlett.xml file for use with a simple test case and it does not work.
ApplicationContext is always null.
this is my test code:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "file:war/WEB-INF/FreedomSpring-servlett.xml" }) public class UserControllerTest { private UserController controller; @Inject private ApplicationContext applicationContext; private String jsonUser = "{ \"username\":\"jonneymendoza\",\"emailAddress\":\" jon@google.com \", \"password\":\"12345678\",\"firstName\":\"jono\", \"surname\":\"richy\", \"country\":\"united kingdom\",\"bio\":\"Bio stuff goes here about the user. where he comes from etc etc. all is well. lets go go go\" }"; @Before public void setup() { controller = new UserController(); assertNotNull(applicationContext); } @Test public void testLogin() { fail("Not yet implemented"); } @Test public void testCreateNewAccount() { ResponseEntity<String> response = controller .createNewAccount(new HttpEntity<String>(jsonUser)); assertEquals(HttpStatus.CREATED, response.getStatusCode()); } @Test public void testGetUserProfile() { fail("Not yet implemented"); } }
Setup error because applicationContext is null. I want to basically load my beans in this case and use them here, but an autwiring object of the UserService class, as shown below in my controller class.
@Controller public class UserController { @Autowired private UserService userService;
xml file that defines my beans service:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <bean id="userService" class="com.jr.freedom.user.UserService"></bean> </beans>
servlet below:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <import resource="mvc-config.xml" /> <import resource="service-config.xml" /> <import resource="datasource-config.xml" /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>database.properties</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <context:component-scan base-package="com.jr.freedom.controllers"></context:component-scan> </beans>
What am I missing?
edit: I get this error on console output
java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted (Lorg / JUnit / feeder / Description; Ljava / languages / Throwable;) V, in org.springframework.test.context.junit4.SpringJUnit4ethlass SpringJUnit4ClassRunner.java:146) in org.junit.internal.runners.JUnit4ClassRunner.runMethods (JUnit4ClassRunner.java:61) in org.junit.internal.runners.JUnit4ClassRunner $ 1.run.unjunnunununrunlassunnerunnerunnerunnerun internal.runners.ClassRoadie.runUnprotected (ClassRoadie.java:34) in org.junit.internal.runners.ClassRoadie.runProtected (ClassRoadie.java:44) in org.junit.internal.runners.JUnit4ClassRunner.run (JUnitjClassRunner.run (JUnitjClassRunner.run (JUnitjClassRunner.run (JUnitjClassRunner.run (JUnitjClassRunner.run) 52) in org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (JUnit4TestReference.java:50) in org.eclipse.jdt.internal.junit.runner.TestExecution.run (TestExecution.java:38) in org .eclipse. unit.runner.RemoteTestRunner.runTests (RemoteTestRunner.java:683) in org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (RemoteTestRunner.javarouble90) in org.eclipse.jdt.inrunnerjun RemoteTestRunner.main (RemoteTestRunner.java:197)
Other editing: fixed the above junit error using junit 4.4 instead. Now we will return to the original issue regarding ApplicatyionContext.
This is a trace of the error stack whenever I run my test:
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html