• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to write JUnit test

 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author & internet detective
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42103
933
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic