I have been searching the net for a simple answer to the question for a few days now. I now want someone to guide me in the right direction. I am a Software Trainee currently working on Enterprise Java Beans.
Also, to establish how confused I am, my mind just doesn’t accept mocking and even stubbing as a proper way of testing what you have or may develop.
You will see as you gain more experience the value of mocking/stubbing. Keep in mind that testing in isolation is the bailiwick of unit-tests. Integration tests are another matter.
Unit testing things in isolation is a good way to establish a baseline of working functionality.
The only way to test in isolation is with mocks/stubs. Otherwise a lot of your tests would end up having to hit the database (in a standard webapp), not to mention you would need to wire up a full stack of components for every test.
Consider a situation where you have to unit test a high level controller. That controller uses a service, which uses 2 DAOs. To test that the controller returned the correct data, you have to either
1) Just mock the service, tell it to expect a call and return some data, and then assert that the correct data is returned.
or
2) Create an instance of the service. Create an instance of each DAO, configured any internal stuff it needs. PUt the DAOs in the service. Populate your database with data.
Which sounds like more work? Keep in mind that the service itself is tested in isolation, as are the DAOs.
Also, testing with mocking/stubbing in mind promotes a cleaner design. It is generally established that loosely coupled, tightly integrated code is a good way to do things. Thinking about how you are going to test things naturally lends itself to that design.