Vikas Gupta: Software architect

Archive for September, 2012

Capturing methods arguments with Mockito

Posted by Vikas Gupta on September 22, 2012

In my project, we developed an API for searching on the lines of Hibernate Restrictions API. It was for Solr search. A sample code looked something like

 public SearchResults searchApprovedRecipes(String searchText, Integer pageNum) {
 SolrSearchServiceImpl.PageCounter pageCounter = new SolrSearchServiceImpl.PageCounter(pageNum);

 SearchCriteria searchCriteria = new SearchCriteria(SearchCriteria.SearchType.RECIPE);
 searchCriteria.add(SearchCriteria.SearchField.NAME, searchText);

 SearchResults searchResults = getRestTemplate().postForObject(getSearchUrl(), searchCriteria, SearchResults.class);

 return searchResults;

Now, if you talk about unit testing, there is nothing much to test, except mocking calls to rest template. The main purpose of this method is to build SearchCriteria. So, if somehow, we can mock SearchCriteria, then we could test that.

Mockito comes with a concept of capturing the arguments passed to the mocked calls (in this case, call to RestTemplate would be mocked), using ArgumentCaptor, which captures argument values for further assertion.

Following is the unit test case of the above method.

 public void testSearchApprovedRecipes() {
 ArgumentCaptor<SearchCriteria> searchCriteriaArgumentCaptor = ArgumentCaptor.forClass(SearchCriteria.class);
 String test = "test";
 solrSearchService.searchApprovedRecipes(test, 1);
 verify(restTemplate, times(1)).postForObject(eq(SEARCH_URL), searchCriteriaArgumentCaptor.capture(), eq(SearchResults.class));

SearchCriteria searchCriteria = searchCriteriaArgumentCaptor.getValue();
 Assert.assertEquals(2, searchCriteria.getSearchFields().size());
 Assert.assertEquals(SearchCriteria.SearchType.RECIPE, searchCriteria.getSearchType());
 Assert.assertEquals(test, searchCriteria.get(SearchCriteria.SearchField.NAME).get(0));

In the above code, you can see that first we created a ArgumentCaptor for the SearchCriteria class, and then, while mocking call to the RestTemplate, instead of passing the SearchCriteria object, we passed the ArgumentCaptor and all the changes made to the SearchCriteria inside the method are captured in the ArgumentCaptor. Now, once the method calls return, we can have assert statements against the ArgumentCaptor. This way, we can test the SearchCriteria.


Posted in TDD | Leave a Comment »

Cleared Cloudera Certification for Apache Hadoop

Posted by Vikas Gupta on September 2, 2012

Today, I cleared Cloudera Certification for Apache Hadoop. I scored 90%. In this blog, I would like to share my preparation and test experience.

Preparation Tips

I started learning Hadoop in June, although I had been procrastinating it for a long time. I bought Hadoop: The Definitive Guide (HTDG) and started reading it. I must admit that I found this book at too much to handle and got a bit distracted by the details mentioned in this book. So, I searched another book and found Hadoop in Action. I must say that hands on approach of this book got me started and I enjoyed trying out the examples provided with this book. I studied first 6 chapters of this book and started feeling confident about writing Hadoop code.

Although, I could write hadoop programs, being an detailed oriented person, I was missing the details provided in the HTDG. So, I picked that up once again and started reading it with zeal and was able to follow the details this time and actually started enjoying the intricacies of the Hadoop framework mentioned in this book. For certification, I read first 8 chapters of this book. I recommend everyone to read Chapeter 11, Pig and Chapter 14, Case Studies to get better equipped and pratical insights about Hadoop.

So, in the end, I would say, for the certification reading the Hadoop: The Definitive guide is enough if you understand the concepts mentioned in this book. Opionally, one may buy Hadoop in Action, as it provides better code samples specially for Joins and Chaining jobs.

Examination Experience

I gave the examination at NIIT Centre at Preet Vihar as it was easily accessible from my home. After the initial setup, which included creating my id and login on the pearson website, I was directed to the computer on which my exam was setup.

As the exam started and I clicked Next, I was expecting some instructions to appear before the exam. But, to my surpise, I got the first question. Just for the future test-takers benefit, I would like to mention that there is a Help button towards left hand side on the bottom of the screen, which can be referred for any help. In this test, you can mark a question for review and also go back and forth and change answers.

Now, about the exam, I would say that the exam was good and some of the questions have fairly close answers and if one has not prepared well/or is not thorough with the concepts, then it is not difficult to falter in this exam. In short, make sure your concepts are thorough, which can be done, either by having practical experience, or as in my case, studying thoroughly and learning concepts of Hadoop.

Once, I was done with the exam, I clicked finish and soon the score flashed. I was happy to see the score, though, I felt, I could have done better.

Some useful links

If you have any questions about preparing for certification or need any other help in Hadoop, you can post a comment or directly email me at

My certificate

For those who want to know how the cloudera certificate looks like, this is the certificate CCDAH certificate.


Posted in Certification, Hadoop | Tagged: , , | 24 Comments »