无法在junit测试中自动装配bean

时间:2016-06-20 08:26:29

标签: java spring spring-mvc junit

这是我的简单测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:root-context.xml")
public class ServiceTest {


    @Autowired
    private CafeService cafeDataService;


    @Test
    public void testCafeService()
    {
        assertTrue(cafeDataService.get()!=null);
    }
}

我的root-context.xml:

<context:component-scan base-package="ru.tenet.cafe" />

<mvc:resources mapping="/resources/**" location="/resources/" />


<beans:bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:array>
            <beans:bean
                class="org.springframework.http.converter.StringHttpMessageConverter">
                <beans:property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
            </beans:bean>
        </beans:array>
    </beans:property>
</beans:bean>

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>


<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

</beans:bean>

<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="jsonMessageConverter" />
        </beans:list>
    </beans:property>
</beans:bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>

<mvc:interceptors>
    <beans:bean class="ru.tenet.cafe.interceptor.LoginInterceptor" />
</mvc:interceptors>


<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="transactionManager" />

<beans:bean id="dataSourceMain" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <beans:property name="driverClass" value="org.postgresql.Driver" />
    <beans:property name="jdbcUrl"
        value="jdbc:postgresql://192.168.101.158:5432/cafe" />
    <beans:property name="user" value="postgres" />
    <beans:property name="password" value="123" />
    <beans:property name="minPoolSize" value="5" />
    <beans:property name="maxPoolSize" value="8" />
    <beans:property name="preferredTestQuery" value="SELECT 1" />
    <beans:property name="acquireIncrement" value="1" />
    <beans:property name="idleConnectionTestPeriod" value="100" />
    <beans:property name="maxStatements" value="0" />
    <beans:property name="checkoutTimeout" value="60000" />
</beans:bean>

<beans:bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSourceMain" />
    <beans:property name="configLocation">
        <beans:value>/WEB-INF/db/hibernate.cfg.xml</beans:value>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.connection.characterEncoding">UTF-8</beans:prop>
            <beans:prop key="hibernate.connection.charSet">UTF-8</beans:prop>
            <beans:prop key="hibernate.connection.useUnicode">true</beans:prop>
            <beans:prop key="hibernate.show_sql">false</beans:prop>
        </beans:props>
    </beans:property>

</beans:bean>



<beans:bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>

这就是我所拥有的:

  

创建名为'ru.tenet.cafe.ServiceTest'的bean时出错:注入   自动连接的依赖关系失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private ru.tenet.cafe.service.CafeService   ru.tenet.cafe.ServiceTest.cafeDataService;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   找到类型为[ru.tenet.cafe.service.CafeService]的限定bean   依赖:预计至少有1个bean有资格成为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

有什么问题?我可以在控制器内自动提供服务,但我的测试不允许我这样做。

0 个答案:

没有答案