nullpoint exception from @Autowired?

Hi,

I am getting an nullpoint exception from an autowired dao instance.
I have a context xml file in the same package where this class is located.
Looks like it can't find this context xml file.

I am using Spring and JUnit 4.1.


-JUnit test
package myproject.datasource;

import static org.junit.Assert.assertNotNull;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class BalanceDaoSpringTest  {

@Autowired
private BalanceDao balanceDao;

@Test
public void testAgainstDaoNull() {

        assertNotNull(balanceDao);

 }
@Test
public void testListBalances() {
       List<Balance> balanceList = null;
       balanceList = balanceDao.listBalances();		
      assertNotNull("SHOULD RETURN BALANCE COUNTS : ",balanceList);	
}
}

Open in new window


-application-context
<?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:tx="http://www.springframework.org/schema/tx"  
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation=" 
            http://www.springframework.org/schema/beans      
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/tx  
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
	
<context:annotation-config />	
<!-- Our dependencies -->
<bean id="balanceDaoBean" class="myproject.datasource.BalanceDaoImpl"/>
<bean id="jobBean" class="myproject.domain.Job"/>


</beans>

Open in new window

dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Before line 13 in your junit test class, you need to add this... (it is what makes the @ContextConfiguration annotation work)
@RunsWith(SpringJUnit4ClassRunner.class)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkim18Author Commented:
Would that work with JUnit 4.1?
I thought that would require JUnit 4.5+.
I will try it.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
It may depend on what version of Spring you are using. What version is that?
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

dkim18Author Commented:
It is not recognizing the
@RunsWith(SpringJUnit4ClassRunner.class)


Do I need to use junit38.AbstractTransactionalJUnit38SpringContextTests?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
It is not recognizing the
What do you mean here? What is the exact error message/stack trace? And can you post the exact version of Spring that you are using.
0
dkim18Author Commented:
spring 3.0.5.

i can usespring 3.1.4 if that will work.
0
dkim18Author Commented:
RuntWith cannot resolved to the type.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Sorry, I seem to always get that the wrong way around, it should have been @RunWith (ie. take out the 's')
0
dkim18Author Commented:
Sorry about that too. I should had known that too.
Now it is working. Thank you.
I didn't use the SpringJUnit4ClassRunner.class because I read somewhere it needs JUnit 4.5+.

Thanks again.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
And yes, it appears that you will have problems with the compatibility of the versions that you have. You may either have to move up to using JUnit 4.5 or greater, or back to Spring versions before 3.0
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.