This week's giveaway is in the Java in General forum.
We're giving away four copies of Java Challengers and have Rafael del Nero on-line!
See this thread for details.
Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Run away! Run away! Here, take this tiny ad with you:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic