Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Autowired not working in JUnit using spring 2.5

 
alpesh helwatkar
Ranch Hand
Posts: 30
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi,
i am using Junit 4.8
spring 2.5
and java 1.6

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
alpesh helwatkar wrote:many thanks for your help christopher!!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad it's solved.
 
jts cool
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic