• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

@Autowired not working in JUnit using spring 2.5

 
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
alpesh helwatkar
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of Java, JUnit, and Spring are you using? And how are you running the tests?
 
alpesh helwatkar
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i am using Junit 4.8
spring 2.5
and java 1.6

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is this constructor ? You are not executing it via JUnit ? Try this instead :

(you don't even need to extend TestCase with Junit4)
 
alpesh helwatkar
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i running it with run as JUnit Test... rightclicking on the JUnit class!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you use the <context:component-scan> tag in your spring-test.xml ?
 
alpesh helwatkar
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes .. like this..<context:component-scan base-package="fi.sok.src" />
 
alpesh helwatkar
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alpesh helwatkar wrote:many thanks for your help christopher!!

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad it's solved.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Team,

 I am also facing same issue. For my Junit test class i have used annotations like this.

@ContextConfiguration(locations = { "file:src/main/resources/templates/fm/mba/db/spring-failure-manager.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class JunitClass{

}

and i am autowiring interface like this and trying to access the methods inside that.

@Autowired
private IFMJobHandler jobHandler;

Getting "Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}"

Please do the needful to resolve this issue.

Thanks
-Prameela

 
Ranch Hand
Posts: 32
1
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I know this one.  The problem is that junits are not run inside the spring container so autowiring simply doesn't happen.  Instead of autowiring the member variable, autowire a setter for the member variable.  Then during your junit test, simply put an object in there yourself by calling the setter as preparation for you test.  This object and be real or mock depending on what you need.
 
Prameela Podigiri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matt Keller,

 Thanks a lot for your reply.This logic is working fine for me.Thanks a lot.

Thanks
-Prameela
 
Prameela Podigiri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The above logic worked for me.But i am creating new object and sending through setter method of property.So it is treating like new object.So the properties inside that class becomes null,which is initialized already in onload. Kindly help  me to resolve this issue.

Thanks
-Prameela
 
Matt Keller
Ranch Hand
Posts: 32
1
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Send in a mock object.  That way you can just mock the getters that you need.  My favorite mock framework is Powermock in combination with Easymock.  Just go google Powermock to learn how to use it.  The docs are pretty good.  They reside in Github now I believe.
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic