我是Spring的新手。我们有Spring Rest API,即模型,控制器和服务。当我输入“mvn install'它将编译,打包并部署到本地weblogic。问题出现在我在com.gm.apms.test
下编写单元测试并使用ExampleControllerTest.java
编写@RunWith(SpringJUnit4ClassRunner.class)
时出现错误,称为ApplicationContext not found
。这个Spring应用程序使用JNDI,我甚至在src\main\webapp\WEB-INF
上写了jdbc连接,文件名是datasource-testcontext.xml
我不知道如何在单元测试运行时使用jdbc直接连接到DB,但使用{{1部署时。任何帮助都非常感激。
ExampleControllerTest.java
JNDI
弹簧-config.xml中
package com.gm.apms.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.gm.apms.common.service.SystemTypesService;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
(
{
"file:src/main/webapp/WEB-INF/spring-config.xml"
}
)
public class ExampleControllerTest {
private MockMvc mockMvc;
@Autowired
SystemTypesService systemTypesService;
@Autowired
private WebApplicationContext ctx;
@Before
public void init() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.ctx).build();
}
@Test
public void checkifSomethingWorkingGood() throws Exception {
mockMvc.perform(get("/services/regions"))
.andExpect(status().isOk());
}
}
这是我得到的错误:
<?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"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">
<!-- base package containing the Spring Controller classes -->
<context:component-scan base-package="com.gm.apms" />
<!-- Spring MVC Annotation -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.gm.apms.common.ContentTypeNegotiatingJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- Sets up the Persistence Context -->
<context:annotation-config />
<!-- JNDI Based datasource -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/APMS" expected-type="javax.sql.DataSource" />
<!-- Spring EntityManagerFactory that is managed by the J2EE Container -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="pu_apms" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
<!-- value="classpath:META-INF/persistence.xml" -->
</bean>
<!-- Injects the EntityManager Instance for methods/fields with the @PersistentContext
annotation -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- JPA Based Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Annotation driven Transaction Manager -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Translator class that maps Javax Persistence Exceptions to Spring JDBC
Exceptions -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="20971520" />
<property name="maxInMemorySize" value="1048576" /> <!-- Decide the file sizes -->
</bean>
<!-- DAO and Service Beans -->
<bean id="salesOrderService" class="com.gm.apms.order.service.SalesOrderServiceImpl"></bean>
<bean id="systemTypesService" class="com.gm.apms.common.service.SystemTypesServiceImpl" />
</beans>
答案 0 :(得分:2)
实现此目的的正确方法是使用Spring对 bean定义配置文件的支持。
像这样定义你的DataSource
bean:
<beans ...>
<!-- JNDI Based datasource -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/APMS" expected-type="javax.sql.DataSource" />
<beans profile="test">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@100.100.100.20:1622:abc" />
<property name="username" value="XXXX" />
<property name="password" value="XXXX" />
</bean>
</beans>
</beans>
然后像这样声明你的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring-config.xml")
@ActiveProfiles("test")
public class ExampleControllerTest {
这应该优雅地解决你的问题! ;)
有关详细信息,请参阅我的Spring 3.1 M2: Testing with @Configuration Classes and Profiles博文。
此致
Sam( Spring TestContext Framework的作者)
答案 1 :(得分:0)
您需要指定@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/path/to/spring-config.xml")
public class ExampleControllerTest {
private MockMvc mockMvc;
@Autowired
SystemTypesService systemTypesService;
@Autowired
private WebApplicationContext ctx;
@Before
public void init() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.ctx).build();
}
@Test
public void checkifSomethingWorkingGood() throws Exception {
mockMvc.perform(get("/services/regions"))
.andExpect(status().isOk());
}
}
答案 2 :(得分:0)
我可以通过调整spring-config.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">
<!-- base package containing the Spring Controller classes -->
<context:component-scan base-package="com.gm.apms" />
<!-- Spring MVC Annotation -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.gm.apms.common.ContentTypeNegotiatingJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- Sets up the Persistence Context -->
<context:annotation-config />
<!-- Spring EntityManagerFactory that is managed by the J2EE Container -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="pu_apms" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
<!-- value="classpath:META-INF/persistence.xml" -->
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/APMS</value>
</property>
<!-- fallback to a local datasource if we are not in the container -->
<property name="defaultObject" ref="developmentDataSource" />
</bean>
<bean id="developmentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@100.100.100.20:1622:abc" />
<property name="username" value="XXXX" />
<property name="password" value="XXXX" />
</bean>
<!-- Injects the EntityManager Instance for methods/fields with the @PersistentContext
annotation -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- JPA Based Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Annotation driven Transaction Manager -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Translator class that maps Javax Persistence Exceptions to Spring JDBC
Exceptions -->
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="20971520" />
<property name="maxInMemorySize" value="1048576" /> <!-- Decide the file sizes -->
</bean>
<!-- DAO and Service Beans -->
<bean id="salesOrderService" class="com.gm.apms.order.service.SalesOrderServiceImpl"></bean>
<bean id="systemTypesService" class="com.gm.apms.common.service.SystemTypesServiceImpl" />
</beans>