使用 Spring 3.1.2、JUnit 4.10.0,这两个版本都相当新。我遇到了无法让基于注释的自动装配工作的问题。
下面是两个示例,一个没有使用注释,它工作正常。第二个使用注释,它不起作用,我找不到原因。我非常关注 spring-mvc-test 的示例。
工作:
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());
}
}
失败,因为userManagementService
为空,没有自动装配:
@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());
}
}
请注意,上面的两个测试类都应该使用相同的上下文配置,并且其中定义了 userManagementService。
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>
并且 UserManagementService (接口)以及 UserManagementServiceImpl 具有@Service
注释。
两个小问题/观察: setup() 永远不会被调用,即使它有 @Before 注释。此外,我注意到如果我的测试方法不以名称“test”开头,则它们不会被执行/识别,尽管我看到的所有 spring-mvc-test 示例都不是这种情况。
pom.xml:
<dependency>
<groupId>org.junit</groupId>
<artifactId>com.springsource.org.junit</artifactId>
<version>4.10.0</version>
<scope>test</scope>
</dependency>
更新:
仅当我从 Maven 运行测试时才会出现此问题;当我从我的 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>