Hello Friends I am caught. I have to write a Junittest for class A which implements interface B(1 method) and Interface C(1 method).
Three Issues/Questions: 1. Class A both methods return void thereby making test for asserts not possible? Please can you tell me any method to write a test for checking state? 2. Class A performActionC() method which is the method which should be called by client or test class catches exception there by we have no clue whether there was an exception thrown By doX or doY methods inside class C. How to now test for exception in Class A 3, How to asser assert whether doX was called or doY was called so that I can confirm that I have tested all code lines and flows. Please can you explain hwo to test ClassA. Thanks Farouk [ October 02, 2006: Message edited by: Bear Bibeault ]
Farouk, 1) If you use a mock object (JMock or EasyMock) for C, you can validate that the correct method on c was called. This tests the logic in performActionB and performActionC. And presumably C was tested in its own unit test. 2) You can tell the mock object to throw an exception. 3) Calling verify on the mock object asserts the appropriate method was called. (In JMock, verify is implied.) You can use a code coverage tool like Emma to verify you have tested all code lines and flows.
Jeanne thanks for your reply 1. My company is not using latest versions of jdk so cannot support easy mock or jmock and prefer to use just JDK. 2. We are ready to mock object by extending original classes to be tested by overiding them with our dummy implementatoins for mocking(exceptions, return values etc). 2. Regarding exceptions, even though I can ask the mock to throw an exception the problem here is that the exception is caught by method performActionB() method inside class A.
Please would appreciate your comments? So how can I test for exceptions in Class A in my test class.
Mohamed, 1) Older versions of easymock and JMock definitely work with Java 1.3 and possibly even further. That said, you can write your own mock/dummy objects if you want. It's just more work. 2) Strongly consider mocking an interface rather than extending classes. It makes the code cleaner and more maintainable. 3) Oh! Is your code supposed to "eat" the exception and just log it to System.out? If so, you can insert a mock/dummy implementation for System.out (System.setOut()) and see if the intended message gets logged.
Joined: Jun 08, 2005
Thanks Jeanne 1. I am ok with the idea of mocking System.out but dont know how? 2. How can I pass the mock of System.out to my class under Test as there is no argument in constructor or setter method available to inject this inside the Classundertest? Thansk Farouk
Mohamed, 1) You can use a ByteArrayOutputStream. This doesn't involve anything being printed to the console and you can later check the output. 2) You just call System.setOut(yourMock). Java itself uses that instead of the regular system out so you don't need to pass anything to your mock.
The test looks something like this:
Joined: Jun 08, 2005
Hello Jeanne Many Thanks for your answers.
I found a easy solution for mocking object and verifying void method calls and exceptions just by using simple junit.
This gives you full control on mocked class and does not break encapsulation as well, Let me know what you think? Regards Farouk