my dog learned polymorphism*
The moose likes Spring and the fly likes Spring Basic Question.. Unit Testing Made simple in Spring ? How? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Basic Question.. Unit Testing Made simple in Spring ? How?" Watch "Spring Basic Question.. Unit Testing Made simple in Spring ? How?" New topic
Author

Spring Basic Question.. Unit Testing Made simple in Spring ? How?

vicky ece
Greenhorn

Joined: Jan 03, 2012
Posts: 17
Hi all,

I am new to spring , when i was reading the Spring in Action book, in lot of places i have noticed that Unit Testing Made simple in Spring but i am not able to get how ? , Any one help me , if possible with some simple example
Bhanuprakash Sreenivas
Greenhorn

Joined: Jan 01, 2010
Posts: 8

Here is one explaination I can give.

Suppose you want to test class A , which has references to class B and class C .
Then you are bound to test class B and class C while testing class A , since
we need class B and C to behave as expected.

In that case the basic concept of Unit testing a class will not be valid. So we have
mock objects where we define the expected behaviour of references.

Using DI , Spring lets you map these mock objects declaratively to the references
at runtime. This avoids making any changes to the actual source for the sole
purpose of unit testing.

Correct me if I am wrong.

Thanks
atul khot
Ranch Hand

Joined: Jul 24, 2008
Posts: 37
At the heart of Spring is dependency injection - Once you start practising DI, at unit testing time you can inject mocks instead of the real things...
Or rather, you first write the tests (as I do - letting the collaborators get discovered in the process)
Look at mockito...
Also, Spring uses POJOs, (A Spring MVC annotation based controller is a simple POJO) - and to test it, Spring already has stuff like MockHttpServletRequest and friends...
So you all almost everything there - just pick it up and use it...
Spring also keeps the code simpler (through AOP) - the code need not be littered with cross cutting concerns (look at @Transactional - and Aspects to add logging to a service class)...
Simpler code (with less class collaborators) - also makes it easy to write tests... (for example, as logging is extrinsic, you don't touch the code, so the unit tests also don't need to be touched)...
All in all, unit testing is easier - Spring is very nice stuff ;-)

My two cents really -

- atul


--cheerio atul
vicky ece
Greenhorn

Joined: Jan 03, 2012
Posts: 17
Bhanuprakash Sreenivas wrote:
Here is one explaination I can give.

Suppose you want to test class A , which has references to class B and class C .
Then you are bound to test class B and class C while testing class A , since
we need class B and C to behave as expected.

In that case the basic concept of Unit testing a class will not be valid. So we have
mock objects where we define the expected behaviour of references.

Using DI , Spring lets you map these mock objects declaratively to the references
at runtime. This avoids making any changes to the actual source for the sole
purpose of unit testing.

Correct me if I am wrong.

Thanks




Thanks I got It......
vicky ece
Greenhorn

Joined: Jan 03, 2012
Posts: 17
atul khot wrote: At the heart of Spring is dependency injection - Once you start practising DI, at unit testing time you can inject mocks instead of the real things...
Or rather, you first write the tests (as I do - letting the collaborators get discovered in the process)
Look at mockito...
Also, Spring uses POJOs, (A Spring MVC annotation based controller is a simple POJO) - and to test it, Spring already has stuff like MockHttpServletRequest and friends...
So you all almost everything there - just pick it up and use it...
Spring also keeps the code simpler (through AOP) - the code need not be littered with cross cutting concerns (look at @Transactional - and Aspects to add logging to a service class)...
Simpler code (with less class collaborators) - also makes it easy to write tests... (for example, as logging is extrinsic, you don't touch the code, so the unit tests also don't need to be touched)...
All in all, unit testing is easier - Spring is very nice stuff ;-)

My two cents really -

- atul



Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring Basic Question.. Unit Testing Made simple in Spring ? How?
 
Similar Threads
Spring and declarative transactions
InternalResourceViewResolver failing JUnit test -- Spring Demo Source
unit testing
Automatic Mocking with Spring framework
IBM RAD Component testing