Announcement Announcement Module
Collapse
No announcement yet.
Autowiring / DI seems to be not working for me with JUnit4.4 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring / DI seems to be not working for me with JUnit4.4

    Hi,
    I want to have both Parameterized & SpringJUnit4ClassRunner but it seems to be not possible.

    So I thought I will make use of just Parameterized class without having SpringJUnit4ClassRunner methods/annotations and load the spring context using ContextConfiguration. But autowire / DI is not working for me and I got

    java.lang.NullPointerException
    at com.test.MyServiceTest.testGetData(..)
    because service object is null

    Code:
    //@RunWith(SpringJUnit4ClassRunner.class)
    @RunWith(Parameterized.class)
    @ContextConfiguration(locations = { "classpath:config/appContext.xml" })
    public class MyServiceTest { 
    	
    	@Autowired
    	@Qualifier("service")
    	private MyService service;	
    	private String code;
    	
    	@Parameters
    	public static Collection<String[]> data() {
    		return Arrays.asList(new String[][] { 
    				{ "XX"},
    				{ "YY"}});
    	}
    	public MyServiceTest(String cd) { 
    		this.code = cd;
    	}
    	@Test
    	public void testGet() {
    		try {
                             List mod = service.getData(code);
    			Assert.notEmpty(mod);
    		} catch (Exception e) {
    		}		
    	}
    }
    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:p="http://www.springframework.org/schema/p"
    	xmlns:aop="http://www.springframework.org/schema/aop"	
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	    <property name="locations">
    	    	<list>
    	    		<value>
    	    			file:/${APP_DIR}/x.properties
    	    		</value>
    	    		<value>
    	    			file:/${APP_DIR}/y.properties
    	    		</value>
    	    	</list>
    	    </property>
     </bean> 		
     
    	<bean id="service" class="com.test.MyServiceImpl" 
    		p:numberOfItems="${NUM_OF_ITEMS}" 
    		p:timeout="${TIMEOUT}" 
    	/>
    
    </beans>
    Code:
    public class MyServiceImpl implements MyService {
    
    String numberOfItems;
    String timeout;
    
     public List getData(String code) {
     ...
     }
     
    // getters, setters goes here
    }

    But if I uncomment the line, @RunWith(SpringJUnit4ClassRunner.class) and comment the Parameterized code, DI is working. Am I doing something wrong?

    Thanks.

  • #2
    You really need the SpringJUnit4ClassRunner because that is what is interpreting the ContextConfiguration with the classrunner that annotation is pretty much useless.

    Comment


    • #3
      Multiple runners

      Thanks Maten. Because I need that for sure I googled and found a solution at http://jaoo.dk/dl/qcon-london-2008/s...withSpring.pdf & http://bmocanu.ro/blog/2010/07/combi...s-with-spring/ and it works!!

      But I have another question.
      Is it possible to get new service (of MyService that is autowired) object for every parameterized @Test call?

      Thanks.

      Comment


      • #4
        You could but that would mean reloading the applicationcontext each time which would notably slow down your tests... Your service should be, in general, a stateless singleton so there shouldn't be a problem.

        How to reload the context is explained in the reference guide, so I suggest a read of the test chapter.

        Comment

        Working...
        X