• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
vicky ece
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vicky ece
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic