aspose file tools*
The moose likes Testing and the fly likes How to write JUnit test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to write JUnit test" Watch "How to write JUnit test" New topic
Author

How to write JUnit test

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Hello Friends
I am caught. I have to write a Junit test 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 ]

SCJP, SCWCD, SCBCD, SCEA 5
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
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.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

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.
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

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:
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

Yes, that is the mock objects pattern.

One thing though: collaborator.assertCalled() should call the Assert.assertEquals() method instead of just returning a boolean. How will you know if it fails?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write JUnit test