Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Junit test issue

 
anish jain
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
Mac Scala VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
?? 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.


 
John McParland
Ranch Hand
Posts: 92
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic