This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Spring Junit test issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Junit test issue" Watch "Spring Junit test issue" New topic
Author

Spring Junit test issue

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
Hi All,

I am creating Junit test cases in Spring.


Here BookingProcessService is an interface and its implementation class is BookingProcessServiceImpl.java.
In spring configuration files, there is no bean id defined for that.
Is there any way I can use the 'bookingProcessService' for invoking the actual method definition written in BookingProcessImp.java in my test methods?
Kaspar Christenson
Greenhorn

Joined: Feb 26, 2009
Posts: 21

If there is no bean with an id "bookingProcessService" wouldn't you need to use appContext.getBean(BookingProcessService.class); to get the bean?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

?? Why are you using BeanFactory at all? And why are you loading your configuration classes like that? I am not even sure where to start, I think you need to understand the fundamentalls of junit and the spring-test framework.

Also I see you have dao and hibernate stuff in there which means it is more of an integration test than a unit test. You would also need to use something like dbunit and/or an embedded db for that, otherwise you should be mocking your repositories and testing only a piece of the service code. I prefer to us a framework like Mockito and java config to return mock beans. You can just @Autowirre in you dependencies.

You should generally create small test applicationContext-test xmls (or java config files) for use with your test cases defining only the beans you need.

I would google around for some examples but it should look more like this.



[How To Ask Questions][Read before you PM me]
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

I agree with Bill's approach here. This was the class is annotated with the configuration files to load and the bean to test is autowired. You could also have a method for setting the object to test and annotate that instead.

It cuts down the amount of code in the test case, making it much clearer.


Passed: SCJP 6 (90%), SCJD 6
Other: Spring training, extensive Swing experience
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Spring Junit test issue
 
Similar Threads
spring dependency injection not working with Selenium
applicationContext.xml locating problem
Not able to understant object initialisation
Testing main() / Hiding static method
Confused about bean's lifecycle method chain