aspose file tools*
The moose likes Spring and the fly likes @Autowired not working in JUnit using spring 2.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "@Autowired not working in JUnit using spring 2.5" Watch "@Autowired not working in JUnit using spring 2.5" New topic
Author

@Autowired not working in JUnit using spring 2.5

alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

Hi,

I am testing my java code using JUnit (Spring Framework) for which i am trying to @Autowire a bean into my JUnit class.

but not able to do so.

when i am including this in my JUnit code:
@Autowired
className classObject

and when i try to access the class methods like:

classObject.classMethod()

I am getting a nullpointerexception at classObject.classMethod();
while invoking the methods of the autowired bean..

the classMethod() is trying to make a connection to the database through webservices in the className class which runs fine when run through server.

also, i have not created a bean in the name of "className" in my application.xml file.Is that required for autowiring in Junit?


Any help will be much appreciated
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

For autowiring to work, it has to happen through a Spring configuration - read Section 9.3.5 - Spring TestContext Framework of the Spring reference, it shows how to load and autowire unit test resources from Spring configurations.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

Hi Nathan,

I referred the document and tried the @Autowiring doing this

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"spring-servlet.xml"})// apprrently @ContextConfiguration("spring-servlet.xml") does not works

public class myTest extends TestCase
{

@Autowired
private Service serviceObject;

public myTest(String name)
{
try{
data=serviceObject.getMethod();
}catch(Exception e)
{
//SOP
}
}

}

Still it gives nullpointerexception at "data=serviceObject.getMethod();"



m i going wrong somewhere?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

What version of Java, JUnit, and Spring are you using? And how are you running the tests?
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

Hi,
i am using Junit 4.8
spring 2.5
and java 1.6

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What is this constructor ? You are not executing it via JUnit ? Try this instead :

(you don't even need to extend TestCase with Junit4)


[My Blog]
All roads lead to JavaRanch
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

I tried without extending the TestCase.. but the assertEquals method gives compilation error... also i have included JUnit 4.8 jar... but when i remove the extends TestCase and comment assertEquals... it says running with JUnit 3!!

heres the revised code
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"spring-servlet.xml"})
public class MyTest {

@Autowired
private Service serviceObject; //Service is an interface which is marked like @Service("serviceObject") and the implementing class ServiceImpl has the method implementation.. which i am trying to access.

@Test
public void myTest() {
popuilateData pd = new popuilateData();
pd=serviceObject.getMethod()//trying to access Service method here... but getting null pointer exception! at this line
Assert.notNull(serviceObject);
}
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

How are you executing this program ? If it were via JUnit, I think you'd get a BeanCreationException if the autowiring failed.

(please UseCodeTags the next time you want to post some code)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

//Service is an interface which is marked like @Service("serviceObject") and the implementing class ServiceImpl has the method implementation..

Isn't the implementation supposed to be marked with @Service("serviceObject") instead ?
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

yes the implementation is marked rightly so too!

i tried manually autowiring like:

Service serviceObject

ApplicationContext context = new ClassPathXmlApplicationContext("spring-servlet.xml");
context.getAutowireCapableBeanFactory().autowireBean(serviceObject);

serviceObject.getMethod();//still gives nullpointerexception!
here it says instances cannot be null

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please answer this : how are you executing this program ? You're saying that you are using JUnit. How are you launching your JUnit test ?
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

i running it with run as JUnit Test... rightclicking on the JUnit class!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I can't find anything else wrong. If Spring doesn't find your context file, an exception should be thrown. If the autowiring failed, an exception should be thrown. It looks like Spring is not even being used ! One reason could be that you're not using JUnit4 after all.

You say that you are "right-clicking" the file. Which editor are you using ?
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

I am using Rational Software Architect built on eclipse for the same.

i am running the class as an independent JUnit class..

the autowiring failure message is not thrown,just that it gives a nullpointer exception!

if it is not throwing an autowired exception that means that bean should be autowired!...

alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

alpesh helwatkar wrote: i tried printing the bean object
@Autowired
serviceClass serviceObject
SOP(serviceObject);//this itself gives null!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

alpesh helwatkar wrote:I am using Rational Software Architect built on eclipse for the same.

You have to check your running configuration and check that JUnit 4 is the test runner being used.

if it is not throwing an autowired exception that means that bean should be autowired!...

Not necessarily. It can also mean that Spring is not being used, so there's no autowiring being performed.
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

i guess by adding this:@RunWith(SpringJUnit4ClassRunner.class) at the beginning of the class already makes sure that it is using JUnit 4!

also ... m running JUnit the way i mentioned before i.e Run as JUnit class... i hope that is the correct approach... do i have to explicitly create a bean in the spring-servlet.xml file? i did that too.. no use though.!!

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

alpesh helwatkar wrote:i guess by adding this:@RunWith(SpringJUnit4ClassRunner.class) at the beginning of the class already makes sure that it is using JUnit 4!

No. Putting the JUnit 4 libraries in your classpath will allow you to import the class, but it doesn't mean that you are using the correct library at runtime. Check your execution configuration. I don't know about Rational Software Architect, but in Eclipse it's in the Run/Run Configurations... menu.

i hope that is the correct approach...

It depends on which library is being picked up. You said in a previous post that it was displaying "JUnit 3", which is the wrong library.

do i have to explicitly create a bean in the spring-servlet.xml file? i did that too.. no use though.!!

You don't need to. As long as you have the <context:component-scan> tag in there.
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

i think we are gogin somewhere...
i checked the run configurations.. it was using JUnit 3
i changed it to JUnit 4 applied and then run the class...
now it is giving
Failed to load ApplicationContext
caused by file not found Exception:
Spring-servlet.xml!!!

the file is under src folder... so i have wrote..

@ContextConfiguration(locations={"src/liiraWeb-servlet.xml"})... is it correcT?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I would not put it under src, but under some folder used for resources, or any folder included in your classpath. Anyway, if you insist on leaving it in "src", make sure that your "src" folder is in your classpath (that's something you'll have to configure in the running configuration again), and change your annotation to :

alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

i've got configured the spring-servlet.xml file, now the error it gives is could not autowire bean serviceObject..


serviceClass is an interface which is implemented by serviceClassImpl,whose methods i m trying to access



Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Did you use the <context:component-scan> tag in your spring-test.xml ?
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

yes .. like this..<context:component-scan base-package="fi.sok.src" />
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30


got it solved...

actually in my spring-servlet.xml file..
the connection factory was locating to the jndi...

since it was not making the jndi connection from standalone JUnit...

the autowiring couldnt took place...

now will have to figure out the connection factory though!
alpesh helwatkar
Ranch Hand

Joined: Apr 06, 2009
Posts: 30

alpesh helwatkar wrote:many thanks for your help christopher!!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I'm glad it's solved.
jts cool
Greenhorn

Joined: Mar 27, 2011
Posts: 1
Hi, I am stuck on the same issue but I'm using Spring 3.0.5 with JUnit 4.8. It's exactly the same issue though. For some reason the ApplicationContext for a single Test class is not established. And this is where the issue is. If you could please give me some feedback on how you solved it would be awesome. Code sample would be really appreciated.

Thanks mate
Obed Vazquez
Greenhorn

Joined: Jan 26, 2012
Posts: 2
You should take a look at this:

http://stackoverflow.com/questions/4746110/autowired-annotation-not-able-to-inject-bean-in-junit-class/10155783#10155783



http://javahelp.redsaltillo.net
 
Consider Paul's rocket mass heater.
 
subject: @Autowired not working in JUnit using spring 2.5