Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Mocking a Single Resource

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to mock a resource that is being accessed using a class full of static methods. I understand this is not a OO way to access but we have all made mistakes. Below is a code extract (written within this text book, so it may not compile ) :

I want to write a Junit test to test doStuff() but mocking EventLogger so that is does not actual log anything to a file. But I am unable to as EventLogger is static.

I suggested that we do the following code changes:


However this suggestion does have a small draw back. I work in project that a number of diffrent people work on. It is likey that people would misuse the static method getEventLogger within the SingletonEventLogger by using it outside the Factory createEventLogger method. Then the any mocking may break. Can anyone suggest a better approach?
Hope you can understand the problem.
Thanks Chris
 
Author
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One other solution (with minimal changes to your original implementation - just enough to let you use a mock objects approach for testing):

This allows to easily unit test doStuff().
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent,
Thanks for the suggestion. When in normal operation would MyClass call setEventLogger? I am right in thinking this is done in the constructor of MyClass?
Chris
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent,
You solution is a lot less work than mine, however it still has the problem that some developers could misuse the DefaultEventLogger by placing direct static class i.e DefaultEventLogger.log(message), all over the code. If there is a quick way of doing things people do it even if they sould not. Once this happens we are back to square one.
I know that this should be picked up in the code review stage but only if the reviwer sees the benifits of unit testing and mocking object (Which is curretly not many).
Thanks
Chris
 
Vincent Massol
Author
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Chris Harris:
Vincent,
Thanks for the suggestion. When in normal operation would MyClass call setEventLogger? I am right in thinking this is done in the constructor of MyClass?
Chris


oops. I forgot the default initialization:

Or it could be in the constructor as you suggested.
 
Vincent Massol
Author
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Chris Harris:
Vincent,
You solution is a lot less work than mine, however it still has the problem that some developers could misuse the DefaultEventLogger by placing direct static class i.e DefaultEventLogger.log(message), all over the code. If there is a quick way of doing things people do it even if they sould not. Once this happens we are back to square one.
I know that this should be picked up in the code review stage but only if the reviwer sees the benifits of unit testing and mocking object (Which is curretly not many).
Thanks
Chris


This is true for any development best practice that you set. I'd strongly recommend using Checkstyle and defining your custom checks. For this check, it's very easy as Checkstyle 3.1 supports a regexp check, so you can easily say it's forbiddent to call the DefaultEventLogger.log().
 
Chris Harris
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vincent,
Thank for pointing me towards Checkstyle sound very useful. Even better it can be used in Ant and Eclipse.
I have had another look at your solution and have noticed that every class could have to create an instace of DefaultEventLogger they wanted to use it. The solution I suggested only every created one EventLogger object.
Allow this I would suggest doing this. What do you think

[ November 05, 2003: Message edited by: Chris Harris ]
 
Vincent Massol
Author
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Chris Harris:
[QB]Hi Vincent,
Thank for pointing me towards Checkstyle sound very useful. Even better it can be used in Ant and Eclipse.
I have had another look at your solution and have noticed that every class could have to create an instace of DefaultEventLogger they wanted to use it. The solution I suggested only every created one EventLogger object.
Allow this I would suggest doing this. What do you think


Yes, you can use a factory instead. Have a factory interface and then:
 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic