Mockito mock child classes too…

Recently, I had a requirement to mock out the child classes of a parent mocked class. Typically we reach this kind of situation when we want to mock out a view and its components.

I was able to get a small snipper for solving this problem:

   protected T createMock(Class clazz) {  
     return (T) mock(clazz, createAnswer());  
   }  
   Answer createAnswer() {  
     return new Answer() {  
       public HashMap methodObjectHashMap = new HashMap();  
       @Override  
       public Object answer(InvocationOnMock invocationOnMock) throws Throwable {  
         Method method = invocationOnMock.getMethod();  
         if (method.getReturnType().getName().equalsIgnoreCase(“void”)){  
           return null;  
         }  
         if (methodObjectHashMap.get(method) == null) {  
           methodObjectHashMap.put(method, mock(method.getReturnType()));  
         }  
         return methodObjectHashMap.get(method);  
       }  
     };  
   }  

Instead of using the mockito’s mock method to create a mock object of a class, We can use the creatMock method which will get the required mock object with each properties also as mocked objects.

Devcamp chennai

தோன்றின் புகழொடு தோன்றுக அஃதிலார்
தோன்றலின் தோன்றாமை நன்று.

sums up the first devcamp chennai event. For the benefit of all the people, I am translating the above tirukkural.

Be born with fame without that it is better not be born.

Devcamp’s entry into chennai was well recieved with energy and passion representing the true spirit of developers in chennai. The days preceding to the event as well as the day of event was full of excitement.

I will try to capture some of them ….

300+ registrations in the devcamp wiki

45+ volunteers for the event

3 sponsors for the event – Thoughtworks, ZOHO and Orangescape

180 + people present on the day of event

Community represented by people as diverse as 12th standard students, young entrepreneurs, technology evangelists, Industry leaders in technology and hard core dirty-hand devs.

Flavour of chennai’s very own filter coffee in T-shirt

Paper wiki voting

Ideas floated around open source hardware toincubating technologies.

How to make the old wine tastier (discussions on improving existing tech, code…)

Fishbowl and adhoc sessions added a new taste to it and was loved by devcampers.

Paper bags to be a small part of environment friendliness.

Others can be captured and relived in the twitter world. Search for #dcc1 or Devcamp chennai.

A question left to be answered… When will we meet again in a unconference like this….

A very worried Indian

As the day passed by today, a very important thing happened in the country. Army was called in to control the worsening security situation in Kashmir.

This was clearly one of the toughest decisions for the government with respect to J & K. Already the situation is heated up due to the spate of violence and clashes between civilians and the security forces.

Even though i am not directly on ground, I am able to sense that the emotions are running high in the valley. When the loss of civilian lives occur for whatever reason, it brings in more worries and problems to tackle.

I wish the presence of the army is short and the sentiments and emotions are brought to normal. Once that is reached, the longer term political solution and the discussion process to rebuild confidence has to be undertaken swiftly.

If institutions/people are guilty, they should also be brought back to book.

A peace in the kashmir valley allows a peaceful india to march forward. With issues of naxalism running in parts of the country and Kashmir valley back in troubled waters, it is a very tough time for the nation in the internal security front.

It is one more challenge which the nation will overcome soon and march towards building a stronger nation.

What is important?

How many times, you have been in situations where there is a dilemma of whether to confront the issues or problems and become a bad guy or just live with it, so that you remain in good books ???

I am able to hear a”YES”. I have been in many times in the same situation.

I decided to do a deep dive of myself and find out what is the thing that i want to do in the future when such a situation occurs.

The first doubt that comes to everyone is whether there are any issues or problems in the current situation. And the next is whether to make your views known to the environment.

I hear… What can i do now? I am also here…….

  1. Accept the reality and live with it.
    • Problems still prevail
    • As you continue living with it, the sensitivity starts diminishing and then sensing itself can become a far fetched dream
    • Poor decisions and poor results will be praised as great achievements
    • Dispassionate relationship to the environment starts creeping in.
  2. Understand the environment and situation
    1. Realize that you have everything to change and change the situation
      • Problems are solved
      • Sensitivity and passion increases urging you to reach better heights
      • Confidence and belief on meritocracy slowly convert themselves in to convictions.
      • Will hurt few people along the path of change
    2. Realize that you have minimal influence and decide that this situation has to be confronted. With that collectively you can bring change
      • Problem solving process is started
      • Sensitivity and passion increases definitely
      • Learning increases and bonding with the environment increases
      • Confidence and belief systems remain intact
      • Confrontation is a hard stance and hence it affects people around you. It earns you a bad guy image

These are not the only ways to approach this situation. surely there are enough minds and ideas which will work out in context.

I want your support in this exploration. Before closing, I just want to quote a small story:

Admiral Jim Stockdale was the highest ranking US military officer imprisoned in Vietnam. He was held in the “Hanoi Hilton” and repeatedly tortured over 8 years. Stockdale told Jim Collins, author of Good to Great, “You must never confuse faith that you will prevail in the end, which you can never afford to lose, with the discipline to confront the most brutal facts of your current reality, whatever they might be.”

Lets keep exploring and help me with your views on how you handled these situations…….

The world is at fault

I recently came across a situation where every bit of problem is attributed to the technology. It started with the criticism that the problems we have are all due to groovy and then matured to grails.

  • The latest entrant into this club is the JUnit 4. There was a issue when we run a grails test suite, one of the integration tests failed. It turned out that the mocking in one of the unit tests were wrong and that left few entries in the database.

What?

A mocked unit test leaving entries in the DB.

Thats the same surprise, i too had. This led us to quoting that Junit 4 is having issues.

I never believed on that and on further investigating, it turned out that when we run the grails unit and integration test together:

  • The test classes are loaded and mocked.
  • The unit test added rows of the mock class.
  • Since the classes are loaded in common for both (some part of it is custom), the mocked class was the same that the integration test also used.

That was the reason that there were so called records left behind in the DB. Actually it never went to the DB itself.

So what made that to be cleared off when we moved to Junit 3 back. Grails adds a metaclass to the GroovySystem.metaClassRegistry when mockDomain, mock… methods are called for the corresponding class. These metaClasses are by default cleared by the tearDown method in the GrailsUnitTestCase.

So when we take up the data setup in Junit 4, we also are given the responsibility of clearing the data back, which can be achieved by a simple call to the GroovySystem.metaClassRegistry clear or the original tearDown call itself.

Ok…. How many times we have been in the same kind of situation and we have just blamed technology for the problems which we introduced…

The problem with this kind of thoughts is:

  • Real problem wont be identified
  • Different forms of the same problem would occur
  • Nothing to learn situations …

In order to effectively address this, We have to move towards a thought process where the problem and its root causes take the centre stage than a opinion that is not backed by data…