Using Spring 3.1.2, JUnit 4.10.0, and pretty new to both versions. I'm having the problem that I can't get the annotation-based autowiring to work.
Below are two samples, the one not using annotations, which is working fine. And the second one using annotation, which doesn't work, and I don't find the reason. I followed the samples of spring-mvc-test pretty much.
Working:
package com.company.web.api;
// imports
public class ApiTests {
@Test
public void testApiGetUserById() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/company/web/api/ApiTests-context.xml");
UserManagementService userManagementService = (UserManagementService) ctx.getBean("userManagementService");
ApiUserManagementController apiUserManagementController = new ApiUserManagementController(userManagementService);
MockMvc mockMvc = standaloneSetup(apiUserManagementController).build();
// The actual test
mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
Failing, because userManagementService
is null, not getting autowired:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration // should default to ApiTests-context.xml in same package
public class ApiTests {
@Autowired
UserManagementService userManagementService;
private MockMvc mockMvc;
@Before
public void setup(){
// SetUp never gets called?!
}
@Test
public void testGetUserById() throws Exception {
// !!! at this point, userManagementService is still null - why? !!!
ApiUserManagementController apiUserManagementController
= new ApiUserManagementController(userManagementService);
mockMvc = standaloneSetup(apiUserManagementController).build();
// The actual test
mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}
Note that both test classes above should be using the same context configuration, and the userManagementService is defined in there.
ApiTests-context.xml:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="user"/>
<property name="password" value="passwd"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:mappingResources="company.hbm.xml">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<context:annotation-config/>
<tx:annotation-driven/>
<context:mbean-export/>
<!-- tried both this and context:component-scan -->
<!--<bean id="userManagementService" class="com.company.web.hibernate.UserManagementServiceImpl"/>-->
<context:component-scan base-package="com.company"/>
<!-- Hibernate's JMX statistics service -->
<bean name="application:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService" autowire="byName"/>
</beans>
and the UserManagementService (interface) as well as UserManagementServiceImpl has the @Service
annotation.
Two minor questions/observations: setup() never gets called, even though it has the @Before annotation. Furthermore, I noticed that my test methods don't get executed/recognized if they don't start with the name 'test', which is not the case though with all spring-mvc-test samples I saw.
pom.xml:
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.10.0</version>
<scope>test</scope>
</dependency>
Update:
The problem only occurs when I run the tests from maven; it's ok when I run the test from within my IDE (IntelliJ IDEA).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
Also note that we can wire other spring beans in our jUnit test classes using @Autowired annotation.
Well, the main difference is that in case u use @Autowired the object is also created, however, it's created by container and container decide when to do that.
Create a Controller Now lets add our class in our Controller. We have create a simple Controller that has the variable userService that has @autowired annotation. For simplicity, we just made the request method to be GET and that the request parameters are included in the URL.
Autowiring wont happen unless you do a component scan.
Why have you commented it out in your code ?
<!--<context:component-scan base-package="com.company"/>-->
Also re:junit. If you're in eclipse can you just go to the dependency tree view of the pom and filter on junit. Check you're actually using that version and not pulling in an older junit.
Edit: Ok I just checked your config and was able to get it working this side. My only guess can be is that you're somehow running it with a bad test runner which is causing it to use the wrong junit.
Edit 2 (SOLVED): So it turns out that the problem is because you are using a custom version of junit. Surefire looks for the provided junit library and cant find it. As a result it defaults to junit 3, which is what causes your app to skip loading the config.
You can explictly specify the custom provider like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
</plugin>
But I have found that it does not work well with custom repos. If possible I would suggest to use the standard version of junit.
Try specific context configuration, e.g.
@ContextConfiguration(locations = {"/file1.xml", "/file2.xml" })
(just showing how this can be used with multiple files when needed - one may be enough)
Edit: have you enabled AutowiredAnnotationBeanPostProcessor as mentioned here? http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With