Announcement Announcement Module
Collapse
No announcement yet.
Junit 4 can't see my beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Junit 4 can't see my beans

    hi, I'm newbie in Spring 3 and after all problems with OSIV, transactions etc. finally i wanted to do some tests with my application. But I can't.
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration (locations = {"classpath:**/db-config.xml"})
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
    
    @Configuration
    public class SferaServiceTest {
    
    	@Autowired
    	private SferaService sferaService;
    
    	@Test
    	public void testSave() {
    		Sfera sfera = new Sfera();
    		sfera.setNazwa("sfera test");
    		sfera.setNumer(99);
    		sferaService.save(sfera);
    		Assert.assertNotNull(sfera.getId());
    	}
    
    }
    I got stacktrace:
    SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener@886b178] to prepare test instance [pl.edm.taskcards.spring.SferaServiceTest@42ed41a7]
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'pl.edm.taskcards.spring.SferaServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private pl.edm.taskcards.spring.SferaService pl.edm.taskcards.spring.SferaServiceTest.sferaServ ice; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [pl.edm.taskcards.spring.SferaService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
    (...)
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [pl.edm.taskcards.spring.SferaService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.raiseNoSuchBeanDefinitionExcept ion(DefaultListableBeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.doResolveDependency(DefaultList ableBeanFactory.java:793)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.resolveDependency(DefaultListab leBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:480)
    ... 28 more
    Here are my xml files:

    web.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app
    	xmlns="http://java.sun.com/xml/ns/j2ee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    	version="2.4">
    
    	<resource-ref>
    		<description>postgreSQL Datasource example</description>
    		<res-ref-name>jdbc/raban</res-ref-name>
    		<res-type>javax.sql.DataSource</res-type>
    		<res-auth>Container</res-auth>
    	</resource-ref>
    
    	<servlet-mapping>
    		<servlet-name>xmlPG</servlet-name>
    		<url-pattern>*.xml</url-pattern>
    	</servlet-mapping>
    
    	<servlet-mapping>
    		<servlet-name>xmlPG</servlet-name>
    		<url-pattern>*.dhtml</url-pattern>
    	</servlet-mapping>
    
    	<servlet>
    		<servlet-name>BackgroundServlet</servlet-name>
    		<servlet-class>pl.raban.servlet.BackgroundServlet</servlet-class>
    		<load-on-startup>10</load-on-startup>
    	</servlet>
    
    
    	<filter>
    		<filter-name>charsetFilter</filter-name>
    		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    		<init-param>
    			<param-name>encoding</param-name>
    			<param-value>UTF-8</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>charsetFilter</filter-name>
    		<url-pattern>*.do</url-pattern>
    	</filter-mapping>
    	
    	<listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
        
    	<context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/db-config.xml
            </param-value>
        </context-param>
    	
    	<filter>
            <filter-name>hibernateFilter</filter-name>
            <filter-class>
               org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
            </filter-class>
            
        </filter>
        <filter-mapping>
            <filter-name>hibernateFilter</filter-name>
            <url-pattern>*.do</url-pattern>
        </filter-mapping>
        
         	<servlet>
    		<servlet-name>spring</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>0</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>spring</servlet-name>
    		<url-pattern>*.do</url-pattern>
    	</servlet-mapping>	 
    </web-app>
    spring-servlet.xml:
    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	(...)
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
    	<context:annotation-config />
    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
    	<bean id="jspViewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/WEB-INF/spring/" />
    		<property name="suffix" value=".jsp" />
    		<property name="order" value="0" />
    	</bean>
    	
        <bean id="pdfViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
         <property name="order" value="1"/>
         <property name="location" value="/WEB-INF/simpleviews.xml"/>
    	</bean> 
    	
    	<bean class="org.springframework.jmx.export.MBeanExporter">
    		<property name="autodetect" value="false" />
    		<property name="assembler">
    			<bean id="jmxAssembler"
    				class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    				<property name="attributeSource">
    					<bean
    						class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
    				</property>
    			</bean>
    		</property>
    
    	</bean>
    	<context:component-scan base-package="pl.edm.taskcards.spring.controllers" />
    	<context:component-scan base-package="pl.raban.resource.spring.controllers" />
    	
    
    	<bean id="messageSource"
    		class="org.springframework.context.support.ResourceBundleMessageSource"
    		p:basename="messages" />
    		
    
    			
    </beans>
    db-config.xml
    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	(...)
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
         
    	<tx:annotation-driven transaction-manager="txManager" />
    	<bean id="txManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    	
    	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName" value="java:comp/env/jdbc/raban"/>    
    		<property name="resourceRef" value="true" /> 
    	</bean>
    	
    	<context:component-scan base-package="pl.edm.taskcards.spring" />
    	<context:component-scan base-package="pl.raban.resource.spring" />
    	<context:component-scan base-package="pl.raban.common.dictionary.spring" /> 
    	
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan">
    			<list>
    				<value>pl.edm.taskcards.spring.entities</value>
    				<value>pl.raban.resource.spring.entities</value>
    				<value>pl.raban.common.dictionary.spring.entities</value> 
    				<value>pl.raban.spring.BaseEntity</value> 
    			</list> 
    		</property> 
    		
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.connection.useUnicode">true</prop>
    				<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
    				<prop key="hibernate.jdbc.batch_size">0</prop>
    				<prop key="hibernate.query.substitutions">true 1, false 0</prop>
    			</props>
    		</property>
    	</bean> 
    </beans>

  • #2
    Junit 4 can't see my beans

    @ContextConfiguration (locations = {"classpath:**/db-config.xml"})

    I would double check if db-config.xml is in the classpath of the test case. If you have it in classpath, just putting it as "classpath:db-config.xml" would suffice

    Comment

    Working...
    X