jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Junit test issue" Watch "Spring Junit test issue" New topic

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

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

Joined: Mar 07, 2010
Posts: 1682

?? 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
I agree. Here's the link: http://aspose.com/file-tools
subject: Spring Junit test issue
It's not a secret anymore!