Oct 27, 2011

Unit testing with mock objects interview questions and answers

Q. Why use mock objects in unit testing?
A. Unit testing is widely accepted as a "best practice" for software development. When you write an object, you must also provide an automated test class containing methods by calling its various public methods with various parameters and making sure that the values returned are appropriate.

When you're dealing with simple data or service objects, writing unit tests is straightforward. However, in reality the object under test rely on other objects or layers of infrastructure, and it is often expensive, impractical, or inefficient to instantiate these collaborators.

For example, to unit test an object that uses a database, it may be burdensome to install a local copy of the database, run your tests, then tear the local database down again. Mock objects provide a way out of this dilemma. A mock object conforms to the interface of the real object, but has just enough code to simulate the tested object and track its behavior. For example, a database connection for a particular unit test might record the query while always returning the same hard coded result. As long as the class being tested behaves as expected, it won't notice the difference, and the unit test can check that the proper query was emitted.

Here are some reasons why mock objects are handy:

  • The unit tests as the name implies must test only a unit of the code and not all its collaborating dependencies. You only have to worry about the class under test. Mock objects allow you to achieve this by mocking external resource and coding dependencies. The example in the next question demonstrates how we can mock reading from a file, which is an external resource.

  • The unit tests need to test for the proper boundary conditions. For example, positive values, negative values, zero value, etc. The mock object make your life easier for mimicking these boundary conditions.

  • One of the biggest mistake one can make in writing quality unit tests is to have state dependencies between unit tests. The unit tests must be able to run in any order. The mock objects will help you isolate these state dependencies, and make your tests isolated and independent.For example
           -- Test the DAO in isolation by mocking the calls to external resources like database, file, etc.
           -- Test your service in isolation by mocking the calls to your DAO.

Having said this, too much mocking can make your code hard to read and understand. So, it is important to have the right balance without overdoing.

Q. How would you go about using mock objects in your unit tests?


Firstly, you need the relevant dependency jar files. A sample pom.xml file is shown below.





The UserDaoImpl is an implementation of the interface UserDao. The implementation read the user names from a text file users.tx.

The user.txt

Peter Smith
Aaron Lachlan
Zara John
Felix Chan

The UserDao interface

package unittest;

import java.util.List;

public interface UserDao {

     public List<string> readUsers() throws UsersException;

The UserDaoImpl class that reads from the user.txt file implements the interface UserDao

package unittest;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class UserDaoImpl implements UserDao {

    private static final String DELIMITER = System.getProperty("line.separator");
    public UserDaoImpl(){}

    public List readUsers() throws UsersException {

        InputStream is = getResource();
        if (is == null) {
            throw new UsersException("users file is not found");

        Scanner sc = new Scanner(is);
        String value = sc.useDelimiter(DELIMITER + "\r").next();

        String[] users = value.split(DELIMITER);

        return (users == null || users.length > 0 ? Arrays
                .asList(users) : Collections. emptyList());

    private InputStream getResource()  {
        ClassLoader cl = Thread.currentThread() .getContextClassLoader();
        InputStream is = cl.getResourceAsStream("unittest/users.txt");
        return is;


Finally the unit test that uses the Mockito framework to mock the actual loading of the user names from text file. The user names will be supplied via the method getDummyIs(). The UserDaoImpl is partially mocked with the spy method. This means the getResource() methods is mocked by supplying some dummy data within the test itself. The readUsers() method is executed from the class under test, which is UserDaoImpl. The getResource() method is mocked to return a user name of "John Patrick" evey time it is invoked.

package unittest.test;

import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import unittest.UserDao;
import unittest.UserDaoImpl;

public class UserDaoWithMockTest {

    public void testGetUsers() throws Exception {
        final UserDao partiallyMockedUserDao = PowerMockito
                .spy(new UserDaoImpl());
                partiallyMockedUserDao, "getResource");
        List<string> users = partiallyMockedUserDao.readUsers();
        Assert.assertEquals(1, users.size());

    @Test(expected = unittest.UsersException.class)
    public void testGetUsersNegative() throws Exception {
        final UserDao partiallyMockedUserDao = PowerMockito
                .spy(new UserDaoImpl());

    @Test(expected = unittest.UsersException.class)
    public void testGetUsers2() throws Exception {
        final UserDao partiallyMockedUserDao = PowerMockito
                .spy(new UserDaoImpl());
                .doReturn(new ByteArrayInputStream("".getBytes()))
                .when(partiallyMockedUserDao, "getResource");
        List users = partiallyMockedUserDao.readUsers();
        Assert.assertEquals(0, users.size());

    public InputStream getDummyIs() {
        String str = "John Patrick";
        return new ByteArrayInputStream(str.getBytes());

Q. What mocking frameworks have you used?

A. Mockito, EasyMock, and PowerMock.

PowerMock is a framework that extends other mock libraries such as EasyMock and Mockito with more powerful capabilities like mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.


Oct 24, 2011

Selenium and Web Driver Interview Questions and Answers

Q. What is Selenium?
A. Selenium is a suite of tools for browser automation (i.e. automated Web testing). It is composed of

  • Selenium IDE: A tool for recording and playing back. This is a Fire-fox plugin.
  • WebDriver and RC provide the APIs for a variety of languages like Java, .NET, PHP, etc. A Java example is shown below. The WebDriver and RC work with most browsers.
  • Grid transparently distribute your tests on multiple machines so that you can run your tests in parallel, cutting down the time required for running in-browser test suites. 

Q. How would you go about using selenium for your web testing?
A. The example below shows the steps involved in testing a simple login page with selenium + Web Driver. The pages have HTML snippets as shown below

Login page:

<form method="POST" action="some/url">

   <input type="text" name="username"/>

   <input type="text" name="password" /> 



Login response page


STEP 1: Configure your Maven to include the required jar file selenium-java-x.x.x.jar.



The selenium-java-x.x.x.jar will transitively bring in the other relevant driver jars for different browsers.

STEP 2: It is a best practice to have separate page objects as these page objects can be shared by multiple JUnit test cases. If a particular page element changes, you will have to change it only in one place, which is page object, and not in all JUnit test cases where a paricular element is used. The JUnit test cases will depend on the page objects and should not refer to the page elements directly.

package myapp.systemtest;

import org.openqa.selenium.WebDriver;

public class SystemTestPage {
    protected WebDriver driver;

    protected SystemTestPage(WebDriver driver) {
        this.driver = driver;

You can have your page objects extend the above generic SystemTestPage class.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import myapp.systemtest.SystemTestPage;

public class LoginPage extends SystemTestPage {
    @FindBy(name = "username")
    private WebElement userNameInput;
 @FindBy(name = "password")
    private WebElement passwordInput;

    @FindBy(xpath ="//input[@type=\"submit\"]")
    private WebElement submitButton;
    public LoginPage(WebDriver driver){
    public void login(String userName, String password) { 
    public String getUserName(String userName) { 
        WebElement element = driver.findElement(By.xpath("//td[text()=" + userName + "]"));
        return element.getText();

STEP 3: Write the JUnit class using the page objects defined above and assert the results. These JUnit test cases can be run to test your web pages.

package com.unittests;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class LoginTest {
    private static final String BASE_URL = "http://localhost:7001/login/login.htm";
    private static final String USER_NAME = "John";
 private static final String PASSWORD = "aa44"
    private WebDriver driver;
    private WebDriverWait wait;
    private LoginPage loginPage; // the page object
    public void setUp() {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 5);
        loginPage = PageFactory.initElements(driver, LoginPage.class);
    public void testLogin() throws Exception {
        loginPage.login(USER_NAME, PASSWORD);
        Assert.assertEquals(MAC, loginPage.getUserName(USER_NAME));

    public void tearDown() {

Q. What are selenium locators? What tools do you use to locate them?
A. Selenium Locators are the way of finding the HTML element on the page to perform a Selenium action on. The example above has a line asshown below to extract the username element from the Login response page. This uses an XPath expression to locate the element.

public String getUserName(String userName) { 
    WebElement element = driver.findElement(By.xpath("//td[text()=" + userName + "]"));
    return element.getText();

The XPath expression will be something like //td[[text()=John] which looks for a td element with text value "John".

The annotation in the above example is also a locator by name as shown below

@FindBy(name = "username")
private WebElement userNameInput;   

This will match the HTML snippet

<input type="text" name="username">

You could also find by tagName, id, css, etc.

There are handy tools to identify the HTML elements or locators.

  • Selenium IDE, which is a Firefox plugin useful in identifying the locators, debugging, etc.

  • The Fire-bug plugin, which allows you to inspect the elements by right clicking on the page and then selecting "Inspect Element". Google chrome provides a similar functionality to inspect element out of the box. 

Q. In your experience, what are some of the challenges with Selenium?
A. In general, badly written test cases whether junit tests or web tests, the biggest complaint is about writing test cases that are not maintainable. Unmaintainable automated test cases can take more time than manual tests. So, it is important to write quality test cases by clearly separting the page objects from the test cases as demonstrated  in the Q&A above. The use of the locators need to be carefully thought through. For example, some frameworks like JSF dynamically generate HTML element IDs. So, if IDs are used in your tests, then the test cases may fail if the IDs have changed. The solution to this problem is to use XPath to find the relevant HTML elements. The ClickAndWait action will not work for AJAX calls, and use "waitForElement" instead.


Oct 21, 2011

JMeter Interview Questions and Answers

Performance testing is an important process of any software development. Even though these are not common interview questions for Java developers, you can bring it up yourself to many open-ended questions. Setting up a performance/load test script is always the first step in fixing non-functional and hard to reproduce issues relating to memory leaks, database deadlocks, thread-safety,  CPU usage, excessive disk I/O, etc.

Q. How did you go about fixing a performance issue?
A. Set up JMeter to reproduce the production like scenario to put through concurrent requests and put the system under heavy load. Used a profiling tool to monitor CPU times, memory usage, etc.

Q. What are some of your recent accomplishments?
A.  Reduced the response time from 4 seconds to 2 seconds. Set up JMeter to put the system under peak load. Used a profiling tool to monitor CPU times, memory usage, etc. Identified the bottle neck, and improved the situation by fixing the offending database connection leak, back tracking regular expression, and a badly constructed SQL query with Cartesian joins.

Q. How would you go about writing a JMeter based performance test case for the following scenario?

  • XML/HTTP based RESTful Web Service calls are made against an order execution system. The system should be able to handle a basic load of 30 orders per 3 min. There will be 30 concurrent users. This means 600 orders per minute.
  • The test case should simulate at least 2 different account ids.
  • 30 orders per 3 min x 60 minutes = 600 orders per hour.

A. The JMeter version 2.4 is used for the following screenshots. The number of artefacts shown for this are as shown below.

STEP 1: Relevant files.

  • OrderExecution.jmx is the JMeter script.
  • 1-OrderRequest.xml and 2-OrderRequest.xml are the two sample XML payloads that will be sent via an HTTP POST (RESTful Web service).
  • TestData.csv is a data file used for retrieving values 1 & 2 to switch between 1-OrderRequest.xml and 2-OrderRequest.xml to simulate 2 different accounts.


<?xml version="1.0" encoding="UTF-8"?>


<?xml version="1.0" encoding="UTF-8"?>



STEP 2: JMeter scripts

If you open the OrderExecution.jmx script file, you will see something like this.

Lets' go through each of the elements. Thread Groups are equivalent of virtual users in other performance testing system. You can add this element by right clicking on the test plan and then selecting Add --> Threads (Users) --> Thread Group. As per the requirements, the system should handle 30 concurrent users. The ramp up time is 180 seconds, which is 3 minutes. All 30 threads will get started in 3 minutes. The loop count is set to 20, to produce 600 orders (i.e. 30 threads * 20 loops) .

It is a good practice to define some variables as shown below, so that these variables can be used in downstream elements without having to hard code. If you want to run against a different host, you can just change it in spot as opposed to having to change it in a number of places. You can add this by right clicking on "Order Processing" and then selecting Add --> Config Element --> User Defined Variables.

The CSV Data Config is a very useful element to read test data from CSV file and then save the read values into some variable. In this example, the values 1 and 2 are read from the file TestData.csv, and stored in a variable named "account". The one iteration will set account to 1, and the next iteration will set it to 2, and then the same assigment repeats since "recycle on EOF" is set to true.

The HTTP Header manager sets the HTTP headers. In this example, the content type is set to application/xml because we are posting the XML files 1-OrderRequest.xml & 2-OrderRequest.xml .

The HTTP request defaults sets the default values so that you don't have to repeat it in each request. Also, note that the user defined variables in the first step are used within ${} as opposed to literal values.

The loop controller can be added to loop through the samplers a number of times. In this example, it is set to 1.

The samplers can be added by right clicking on the "Create Order" loop controller and then selecting Add --> Sampler --> HTTP Request. It shows the path, method, and the file to send. The actual RESTFul web service call will have the URL


Also, note that in the File Path the ${DATA_PATH} is a user defined value, and ${account} is a variable that is assigned from the TestData.csv file via the CSV Data Config element. The account will be dynamically changed between 1 and 2 to retrieve the files 1-OrderRequest.xml and 2-OrderRequest.xml respectively for each iteration.

JMeter supports a number of samplers like SOAP request, FTP request, JDBC request, JMS publisher, JMS subscriber, etc to name a few.

The XPath assertions are used to assert the response for presence of responseCode="SUCCESS" and a valid orderId > 0. The response message will look like

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

The other three elements are reporting elements. There are many reporting elements to choose from depending on the requirements. You can add reporting elements via Add --> Listener.

Finally the Gaussian Timer that will delay the execution by 3 minutes (i.e. 180000 ms) with a standard deviation of 10 seconds. This will ensure the throuput of 30 orders per 3 minutes.

Q. How would you go about automatically recording the your browser actions into JMeter?
A. This can be achieved 2 ways.
  • Use BadBoy, which is a tool to record your Web interactions.The  recorded script can be exported as a JMeter file with the extension .jmx to be imported into JMeter. The generated script will not be pretty, but it can be cleaned up and further extended within JMeter with user defined variables,  CSV data config, HTTP defaults, etc. You can record the script as you use your application.

  • Use JMeter as an HTTP proxy server to record your browser actions.Firstly, set up your JMeter to act as a proxy server on an available port like say 9090 as shown below.
Configure the proxy server as shown below:

  • Port: 9090
  • Target Controller indicates where to record. In the above diagram indicates to capture it in the WorkBench under the proxy server itself. This allows you to later on move the captured elements one by one into the TestPlan
  •  You don't want to capture everything. So, filter it with include and exclude patterns. For example,  Include: .*myapp.*, Exclude: .*\.jpg. 
  • "Start" the proxy server.

You need to set up your browser to use this proxy server.

Finally, start using the browser to record your actions. For example, you could use the following URL


Note: If you use localhost instead of myapp as the host, JMeter may not record your actions. To overcome this, type ipconfig on your DOS prompt and copy the local IP address and use that instead of localhost in your URL.

Q. What are Pre-Processor and Post-Processor elements?  In what order does JMeter process various type of elements?
A. A Pre-Processor executes some action prior to a Sampler Request being made. If a Pre-Processor is attached to a Sampler element, then it will execute just prior to that sampler element running. A Pre-Processor is most often used to modify the settings of a Sample Request just before it runs, or to update variables that aren't extracted from response text.

A Post-Processor executes some action after a Sampler Request has been made. If a Post-Processor is attached to a Sampler element, then it will execute just after that sampler element runs. A Post-Processor is most often used to process the response data, often to extract values from it.

A Regular Expression Extractor can be used as a Post-Processor element to extract values to be used elsewhere in subsequent requests. For example, if you are using JSF and Seam frameworks, the jsfViewState and CID values can be extracted as shown below:

The elements are executed in the following order as per the JMeter documentation.

0. Configuration elements
1. Pre-Processors
2. Timers
3. Sampler
4. Post-Processors (unless SampleResult is null)
5. Assertions (unless SampleResult is null)
6. Listeners (unless SampleResult is null)

Timers, Assertions, Pre- and Post-Processors are only processed if there is a sampler to which they apply. Logic Controllers and Samplers are processed in the order in which they appear in the tree. Other test elements are processed according to the scope in which they are found, and the type of test element.

Q. What are some of the challenges you faced with JMeter?

  • If a subsequent request rely on the cookie set by the previous request, the "cookie manager" element is required.

  • When setting up the "CSV Data Set Config", don't have any spaces in the variable names.

Q. Have you used any features of JMeter that gave you greater flexibility?
A. Yes. The JMeter supports BeanShell scripting for greater flexibility. The BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java.

If you want to output all the order ids extracted out of the response messages for the response messages shown below


An XPath Extractor can be used to extract the "orderId" from the response message and store it in a variable named "orderId" as shown below.

Now, the BeanShell PostProcessor can use the extracted "orderId" variable and write to a file (e.g. C:\\Temp\\order-processing\id.txt ) as shown below.

You could do a lot more powerful things by writing your own embedded scripting with the BeanShell. For example, You can create your own file named MyBeanShell.bshr and copy it to $JMETER_HOME\bin folder. The MyBeanShell.bshr will have functions like


 import java.text.SimpleDateFormat;

 cal = Calendar.getInstance();
 cal.add(Calendar.DATE, -10);
 sdf = new SimpleDateFormat("dd-MMMM-yyyy");
 return sdf.format(cal.getTime());

isTenth(int loopCounter)

 returnValue = false;
 if (loopCounter % 10 == 0)
 returnValue = true;
 return returnValue;

You could invoke these functions from your JMeter elements like

${__BeanShell(return getReportStartDate();)}

Also, note that pre-defined functions like  __time can be used  as shown above. There are also handy JMeter plugins from Google code at containing useful functions, listeners, samplers, etc. Just download the zip file and extract the "JMeterPlugins.jar" into $JMETER_HOME\lib\ext folder. This plugin features are prefixed with jp@gc.

Finally, instead of BeanShell or simple functions out of the box like __time, you could also write your own Java based custom functions using the org.apache.jmeter.functions.Function interface by writing your function  as described here

Q. How do you ensure re-usability  in your JMeter scripts?

  • Using config elements like "CSV Data Set Config", "User Defined Variables", etc for greater data reuse.
  • Modularizing shared tasks and invoking them via a "Module Controller".
  • Writing your own BeanShell functions, and reusing them.

Labels: ,

Oct 18, 2011

Java Interview Questions and Answers on tools

Related reads:
In today's enterprise Java development, you will use plethora of tools to make your life easier and more productive. The open-ended questions described below are not to make the Hire/No Hire decision, but to judge your seniority and experience.

Q. Can you describe some real life scenarios in which tools helped you solve issues?
A. [Hint: The objective is to ascertain, if you are using the right tool for right job. This question can also be phrased differently with How would you go about ....? type of questions like How would you go about fixing a memory or resource leak in a JEE application?, etc ].

1. Debugging: The application and server logs are the first place to turn for any issues in production or during development. It is really worth understanding some of the tools that will help to obtain, analyze and search through the logs.

  • FileZilla or WinSCP to transfer files using FTP, SFTP or SCP protocols. Log files or database extract files can be transferred from a Unix machine to your sandbox (i.e. local machine) using these utilities, and explored further using text editors like Notepad++, UltraEdit, or TextPad. These text editors provide handy features like  search text highlighting, REGEX based searches, column formatting,  compare files, and many more. Notepad++ has powerful editing features, uses less CPU, and opens larger files.  
  • Cygwin, MobaXterm or MSYS to emulate Unix like environment for Windows. Many production systems are Unix/Linux based, and having a Unix/Linux environment on your local environment can make you more productive by harnessing the power of Unix commands.
          Search the log files to see if a particular user logged in.
cat /var/log/server.log | grep "login.jsp" | grep "userid"
         List all the log files that have a particular search text.
grep -l "Missing Characteristic Value" *.log* 

        You can use regular expression patterns to search.
grep -e '^import.*util.regex' *.java

        Find all occurrences of util.* packages except the util.regex package.
grep -e '^import.*util' *.java | grep -v 'regex' 

  • Remote debugging: It is increasingly essential with the globalization to be able to debug a Java application that is deployed remotely, in another country or city. You will come across scenarios where an application might be running fine in your sandbox (i.e. local desktop), but might be buggy when running in another environment or country.

    Say you want to remotely debug an application called MyApp.jar that is running remotely, you can set up your desktop to be able to debug it by enabling the remote debugging as shown below:

    java -Xdebug -Xrunjdwp:transport=dt_socket,address=888,server=y -jar MyApp.jar

    The above command tells the MyApp.jar to start a server socket on port 888, and publish the debugging messages using the jdwp, which stands for Java Debug Wire Protocol. The IDEs like eclipse can be configured to tap in to remote debugging as shown below. 

 In eclipse, select  Run --> Debug Configurations. Right click on "Remote Java Application", and select "New". Fill in the config details as shown below and apply the changes. Click on the "Debug" button to start debugging the remote application within eclipse. You will have to attach the source files from within eclipse.

The "diagnostic-core" is the project with the source files within eclipse. The "MyApp" is the remote debug configuration that can be stopped and started within eclipse. The connection properties provide information as to where to look for debug messages exposed via jdwp. 

    • Web debugging tools like Fiddler2 for IE, Firebug NET tab for Firefox, JMeter HTTP proxy server, Charles web debugging, etc act as a proxy to capture request parameters, request/response content, headers, cookies, etc for debugging purpose.

    • SQL proxy drivers like P6SPY, log4jdbc, etc to log generated SQL statements, execution times, etc.

    • Network packet sniffing tools like Wireshark, Kismet, tcpdump, etc for analyzing the network protocols & issues. Developing applications that interact with web services presents a unique set of problems like not knowing exactly what message was sent to the server, or what response was received. Some of the most difficult bugs to track down are caused by a disconnect between what you think you are sending to the server, and what is actually going across the wire. These tools are commonly called "packet sniffers" and capture all network packets that move across your network interface. Examining the contents of these packets and the order in which they were sent and received can be a useful debugging technique.

      2. Integration testing: Most applications built nowadays need to integrate with internal and external systems through SOAP or RESTful web service and messaging (e.g. JMS). There are a number of handy tools/clients to assist in development, testing, and debugging applications that integrate with other systems.

      • HermesJMS is an extensible console that helps you interact with JMS providers making it simple to publish and edit messages, browse or search queues and topics, copy messages around and delete them.
      • soapUI is a functional testing tool for SOA and Web Service testing. soapUI provides complete test coverage - from SOAP and REST-based Web services, to JMS enterprise messaging layers, databases, Rich Internet Applications, and much more.
      • Firefox poster plugin is a developer tool for interacting with web services and other web resources that lets you make HTTP requests, set the entity body, and content type. For example, you can make RESTful service calls over HTTP by posting XML. 

      3. Version control: CVS and Subversion are the two most prominent open source version control tools. TortoiseCVS and TortoseSVN let you work with files under CVS and SVN version control systems directly from Windows Explorer. With these tools you can directly check out modules, update, commit and see differences by right clicking on files and folders within Explorer. You can see the state of a file with overlays on top of the normal icons within Explorer. It even works from within the file open dialog.

      4. Performance testing: JMeter is a popular performance testing tool for testing web applications, web services (both SOAP and RESTful), and SQL query performance.JMeter Interview Questions and Answers

      5. Hot deployment tools like  JRebel, improves developer productivity by skipping the build and redeploy phases in your sandbox. JRebel is a  JVM agent, which instantly reloads your code upon change without having to restart your application server or redeploy the application. It integrates into your JVM as a classloader extension and enables reloading of classes without requiring to create a new classloader, which is usually the case when redeploying or restarting application servers.

      6. Database administration and SQL execution tools such as db-visualizer, SQuirreL SQLSQL Developer, DBArtisan, and Toad.There are command-line tools like iSQL for Sybase and SQLPlus for Oracle. BCP (stands for Bulk CoPy) and SQL Loader are ETL (i.e. Extract Transform and Load) tools to import data from a flat file into a database and export data from a database into a flat file.

      7. Penetration testing or "PEN test" tools like Google's Skipfish, Firefox plugin "tamperdata", etc to identify security holes in your web application.

      Q. What are some of the frequently used short-cut keys on your favorite IDE?
      A. The less you touch the mouse, the more code you can write. For example, in eclipse, the following short-cut keys are very handy and makes you more productive.

      1. CTRL + SHIFT + R to open a resource and CTRL + SHIFT + T for Java type
      2. F4 on highlighting a class or interface to open the type hierarchy.
      3. CTRL + SHIFT + O to organize imports
      4. CTRL + SHIFT + F to format text.
      5. CTRL + D to delete a row
      6. CTRL + SHIFT + / to comment out code.
      7. CTRL + 1 is probably the most useful one. It activates the quick fix.
      8. CTRL + SHIFT + G to generate getters and setters.
      9. CTRL + O to get a snapshot of the class members. You can go directly to a member (method, variable) of a huge class file with this. If you just want to jump from one member to the next (or previous), you can use Ctrl + Shift + downarrow or Ctrl + Shift + uparrow, respectively.
      10. CTRL + L to go to a particular line. Handy when the stack trace says that the error is on line 256. 
      11. CTRL + SHIFT + L to get the list of all the currently defined short-cut keys.
      12. Pressing down the CTRL key while hovering over a class, interface or a method with the mouse to invoke the contextual menu to navigate to a particular interface, implementation, or method. Very useful.

      Q. Name a feature in your favorite IDE that you discovered recently  or  find it very useful?
      A. In eclipse, the "working sets" are very handy concept to categorize resources across projects into a contextually relevant representation. Working sets are simply, as their name suggests, a sub-set of files, classes, folders or projects with the following benefits.

      • Categorize multiple projects together that may represent a single application. For example, maven multi-module projects like   myproject-core, myproject-web, myproject-batch, myproject-parent, myproject-schedule, etc can be grouped into a single working set named "myproject".

      • You can search or access resources by working sets. You can even close the whole working set.

      • If you like, you can also create working sets by layers -- Model, View, Controller, Service, DAO, etc. It is also possible to select particular packages, classes, and folder structures to be included or excluded from a given working set. This allows developers to organize code anyway they want. 

      • Very handy and useful especially in larger projects.

      You can get the production support team to email you a stack trace, and then you can copy and paste the stack trace into Eclipse's Console window for further analysis. You can then click on class names in the stack trace as if your own code had generated it.

      There are other productivity improving features described above in terms of short-cut keys, especially the ctrl-1 for quick fix.

      The "templates" are very handy for frequently used code snippets. The window --> preferences --> Java --> Editor --> Templates can be used  to add handy code snippets to improve productivity. For example, the following template to declare loggers.

      ${:import(org.apache.commons.logging.Log, org.apache.commons.logging.LogFactory)}
      private static final Log LOG = LogFactory.getLog(${enclosing_type}.class);

      You can name the above template snippet as "logger" and when you are inside your class, if you type "logger" and then ctrl+space, eclipse will generates and inserts the required code based on the saved template.

      private static final Log LOG = LogFactory.getLog(TestSpring3.class);

      Many other useful templates can be created for productivity.

      Other similar links:


      Oct 14, 2011

      Java Interview Questions and Answers on Software Architecture

      Good caliber candidates have the ability to look at the big picture and drill down into details. The line between software development and software architecture is a tricky one. Regardless of you are an architect, developer, or both, one needs to have a good understanding of the overall software architecture. The following Java interview questions are very popular with the interviewers and it can significantly influence the decision  of Hire/No Hire. So, it really pays to have a good overview of various possible architectures. The questions shown below will also make a good platform for further questions depending on your answers.

      Be prepared for a white board session on architectures, especially the bird's eye view of the last application you had worked on. There will be lots of follow on questions like why a particular approach was used?, what are the benefits and drawbacks of a particular approach?, etc. 

      Q. Can you draw me a 1000 foot view of the architecture of the system you were/are involved in, in your current/last position?
      Q. Can you describe the architecture of a medium-to-large scale system that you actually designed or implemented?
      Q. Can you white board the components of the system you recently worked on?
      Q. How would you go about designing a JEE shopping cart application?
      Q. Can you discuss some of the high level architectures you are experienced with?

      A. There are a number of high level conceptual architectures as discussed below. These individual architectures can be mixed and matched to produce hybrid architectures.

      Model-View-Controller  Architecture

      Most web and stand-alone GUI applications follow this pattern. For example, Struts and Spring MVC frameworks and Swing GUI.

      The model represents the core business logic and state. The view renders the content of the model state by adding display logic. The controller translates the interaction with the view into action to be performed by the model. The actions performed by a model include executing the business logic  and changing the state of the model. Based on the user interactions, the controller selects an appropriate view to render. The controller decouples the model from the view.

      Service Oriented Architecture (SOA)

      The business logic and application state are exposed as reusable services. An Enterprise Service Bus (ESB) is used as an orchestration and mediation layer to decouple the applications from the services. 

      The above architecture has 5 tiers. The application tier could be using a typical MVC architecture. The service orchestration tier could be using ESB products like Oracle Service Bus, TIBCO, etc and BPM products like Lombardi BPM, Pega BPM, etc. In the above diagram, the ESB integrates with the BPM via messaging queues. The service tier consists of individual services that can be accessed through SOAP or RESTful web services. The SOA implementation requires change agents to drive adoption of new approaches. The BPM, application integration, and real-time information all contribute to dynamically changing how business users do their jobs. So, it needs full support from the business, requiring  restructuring and also it can take some time to realize the benefits of SOA. Cloud computing is at the leading edge of its hype and as a concept compliments SOA as an architectural style. Cloud computing is expected to provide a computing capability that can scale up (to massive proportions) or scale down dynamically based on demand. This implies a very large pool of computing resources either be within the enterprise intranet or on the Internet (i.e on the cloud).

      User Interface (UI) Component Architecture

      This architecture is driven by a user interface that is made up of a number of discrete components. Each component calls a service that encapsulates business logic and hides lower level details. Components can be combined to form new composite components allowing richer functionality. These components can also be shared across a number of applications. For example, JavaScript widgets, Java Server Faces (JSF) components, etc.

      RESTful data composition Architecture

      The user interface can be built by calling a number of underlying services that are each responsible for building part of a page. The user interface translates and combine the data in different formats like XML(translate to HTML using XSLT), JSON (Java Script Object Notation), ATOM (feed for mail messages and calendar applications), RSS (for generating RSS feeds), etc.

      HTML composition Architecture

      In this architecture, multiple applications output fragments of HTML that are combined to generate the final user interface. For example, Java portlets used inside a portal application server to aggregate individual content..

      Plug-in Architecture

      In this architecture, a core application defines an interface, and the functionality will be implemented as a set of plug-ins that conform to that interface. For example, the the Eclipse RCP framework, Maven build tool, etc use this architecture.

      Event Driven Architecture (EDA)

      The EDA pattern decouples the interactions between the event publishers and the event consumers. Many to many communications are achieved via a topic, where one specific event can be consumed by many subscribers. The EDA also supports asynchronous operations and acknowledgments through event messaging. This architecture requires effective monitoring in place to track queue depth, exceptions, and other possible problems. The traceability, isolation, and debugging of an event can be difficult in some cases. This architecture is useful in scenarios where the business process is inherently asynchronous, multiple consumers are interested in an event(e.g. order status has changed to partially-filled ), no immediate acknowledgment is required (e.g. an email is sent with the booking details and itinerary), and real-time request/response is not required (e.g. a long running report can be generated asynchronously and made available later via online or via email).

      Most conceptual architectures use a hybrid approach using a combination of different architectures based on the benefits of each approach and its pertinence to your situation. Here is a sample hybrid approach depicting an online trading system.

      FIX is a Financial Information eXchange protocol. You could also notice a number of synchronous calls using XML/HTTP or SOAP/HTTP and asynchronous calls using JMS. The above diagram also depicts that an enterprise architecture can be complex with a number of moving parts. So, it is imperative that all these moving parts are properly monitored and tested for any potential performance issues. Most of these services will be running as a cluster or a load balanced service with either active/active or active.passive configuration for high availability and scalability.  

      Other Relevant Design Questions and Answers


      Java interview questions and answers on code quality

      Open-ended interview questions can tell a lot about a candidate.Code quality starts when you interview someone. You need the right people with the right skills and most importantly the right attitude. If you don't know how to achieve good code quality or don't care about it then your chances of job interview success will be very slim if not zero. The open-ended job interview questions give you a great opportunity to sell not only your technical skills, but also your soft-skills and personal traits. Impressive answers in open-ended questions can motivate your prospective interviewers to overlook any short-comings in your resume like not knowing a particular framework or tool or not having any experience with a particular application server. The following questions are very frequently asked, and your answers will be drilled down. So, don't just rely only on memorizing the answers given below, but apply it.

      Q. How do you ensure code quality in your application?
      A. code quality means writing readable and robust code, that conforms as much as possible to the style-guideline that is used, and that has as little as possible defects. It also means writing maintainable code with proper automated and manual tests.

      1. Write a number of automated tests

      • Unit tests  using JUnit or TestNG. For unit tests use mock objects to ensure that your tests don't fail due to volatility of the data changes. There are mocking frameworks like EasyMock, Mockito, and PowerMock.
      • Integration testing of your services with JUnit or TestNG. Your integration tests are only as good as the quality of the data. You could either use dedicated test databases or use frameworks like DBUnit to manage extraction and insertion of data.
      • Web testing  using Selenium + WebDriver. Selenium + WebDriver ( Selenium interview questions and answers) allows you to reenact web user experience and run it as an automated unit test using JUnit or TestNG.Your tests are only as good as the quality of the data. You could either use dedicated system test databases or use frameworks like DBUnit. DBUnit allows you to extract the data from databases into flat XML files, and then refresh (i.e. insert or update) the data into the database during setup phase of running the unit tests. There are handy proxy JDBC driver tool called P6SPY, which logs the SQL queries that are executed against the database by the DBUnit. This P6SPY also very handy in debugging Hibernate‚Äôs generated SQL  by acting as a proxy driver between JDBC and the real driver so that all generated SQL will be logged. There are other Web testing tools like Badboy.
      • Load testing your application with tools like JMeter, OpenSTA, etc.  The Badboy compliments JMeter by allowing you to record scripts and then exporting the scripts as a JMeter file to be used in JMeter.JMeter Interview Questions and Answers

      2. Have regular code reviews. There are tools like Crucible from Atlassian that gives your team an efficient way to benefit from the power of constant code review with features like inline commenting, simple workflow, asynchronous reviews, email and RSS notifications, JIRA integration and much more.

      3. Using a number of code quality tools.

      • Checkstyle ensures the style of your Java code is standardized and "nice". It checks white spaces, new lines, formatting, etc. (i.e. it looks on the code line by line).  This only ensure style of your code.
      • On the other hand there is PMD which not necessarily checks the style of your code but it checks the structure of the whole code. PMD scans Java source code and looks for potential problems like possible bugs, dead code, suboptimal code, overcomplicated expressions, duplicate code, etc.
      • FindBugs is a static analysis tool to look for bugs in Java code. It discovers possible NullPointerExceptions and a lot more bugs.
      • Sonar is a very powerful tool covering 7 axes of code quality as shown below.
      (The diagram is from the Sonar website)

      4. Using continuous integration servers (on a clean separate machine) like Bamboo, Hudson, CruiseControl, etc to continuously integrate and test your code.

      5. Not stopping to code once the code works. Too many developers feel their job stops at making something happen. It is a best practice to constantly refactor code with proper unit tests in place.

      Q. Do you use test driven development? Why / Why not?
      A. [Hint] Yes.

      • Gives you a better understanding of what you're going to write.  Gets you to clearly think what the inputs are and what the output is.  Helps you separate the concerns by getting you to think about the single responsibility principle (SRP).
      • Enforces a better test coverage. This gives you the confidence to refactor your code in the future, since you have a good coverage.
      • You won't waste time writing features you don't need.

      Q. How do you keep your knowledge up to date about writing quality code?
      A. Mainly through good books and online resources.

      Through good books
      • Code Complete: A Practical Handbook of Software Construction by Steve McConnel
      • Clean Code:  A Handbook of Agile Software Craftsmanship by Robert. C. Martin

      Through good articles and  quick tips at online resources like
      •   JavaLobby (
      •   handy blogs on Mockito, PowerMock, Easy mock, DBUnit, Selenium, JUnit, TestNG, etc. 

      Q. What do you look for when you are reviewing others' code?
      A. Firstly, and most importantly look to see if the following key areas are properly adhered to avoid any potential issues relating to thread-safety, performance, memory leak, scalability, transaction management, exception handling, etc. Also, look for the key areas like best practices, design concepts, and design patterns. Key Areas.

      Secondly, ensure that proper test cases are written. Also, ensure that it has a good code coverage.

      Here are a few finer points:
      • Naming conventions.
      • Existence of unused code and commenting code out. Delete unused code. Source control is there for maintaining the history of your code.
      • Unnecessary comments.  The method and variable names must be self explanatory without cluttering the code with excessive comments. The methods should do only one thing with fewer lines of code. More than 15-20 lines of code is questionable. The number of parameters passed in must also be small. The public methods must fail fast with proper validations.
      • Repeated code due to copy-paste. For example, same logic or hard coded values repeated in a number of places.
      • Favoring trivial performance optimization over readability and maintainability. 
      • Tightly copuled code. For example, not coding to interfaces, favoring inheritance over composition, etc.
      • Badly defined variable scopes or variable types. For example, using a data type double to represent monetary values instead of BigDecimal. The variable scopes must be as narrow as possible.
      • Using mutable objects and read only varaibles where immutable objects make more sense.
      • Proper implementation of language contracts. For example, not properly implemented equals and hashCode methods.
      • Deeply nested loops or conditionals. Nested loops can be replaced with changing the logic or through recursion. Nested if-else conditionals are a good candidate for applying polymorphism.
      • Not properly handling exceptions. For example, burying exceptions, exposing internal exception details to the users without proper friendly messages at the Web  layer, etc.
      • Badly written unit tests.
      • Not designing the classes and interfaces with proper design concepts and principles. For example, strongly coupled classes, classes trying to do more things than it should, modelling it as a class when it should be an attribute, etc.
      • Not handling and testing non-functional scenarios. For example, not properly handling service timeouts or using incorrect timeout values.
      • Reinventing the wheel by writing your own implementation, when there is already a proven and tested  implementation provided by the API.
      Note: The Core Java Career Essentials cover all the above in detail with examples.

      Q. There are lots of advantages in writing tests, but in your experience, are there any disadvantages?
      A. [Hint: Everything has pros and cons. Discussing the cons demonstrates your experience, but make it a point to mention how you overcome the cons to exemplify your problem solving, researching, and analytical skills.

      Tests need to be maintained. If a particular method signature or logic is changed, then all the relevant tests need to be fixed. Many developers don't put lots of thoughts into writing quality test cases through proper what if analysis. For example, what if new records are added to the database? What if the data is missing?, etc. So, this continuous tweaking of test cases will require time. Writing quality tests require experience and lots of need to have an enthusiastic team and at least one experienced developer who knows how to write good tests, and also knows a few thing about good architecture

      Requires some initial upfront investment in time and cost. But, this initial up front investment will pay-off  significantly in a long run through quality code and robust application.

      Q. What is the difference between fake objects, mock objects, and stubs?

      • Fake objects build a very lightweight implementation of the same functionality as provided by a component that you are faking. Since they take some shortcut, they are not suitable for production.
      • Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive. You can use mocking frameworks like EasyMock, Mockito, PowerMock, etc to achieve this.When an actual service is invoked, a mock object is executed with a known outcome instead of the actual service. With mock objects, you can verify if expected method calls were made and how many times. The verify(mockOrderDao) shown below tells EasyMock to validate that all of the expected method calls were executed and in the correct order.

      public void testOrderService() {
              expect(mockOrderDao.getOrders(...)) .andReturn(mockResults);
              assertTrue(service.login(userName, password));
              verify(mockDao);  //expected method calls were executed 

      • Stubs are like a mock class, except that they don't provide the ability to verify that methods have been called or not called. Generally services that are not ready or currently not stable are stubbed to make the test code more stable.

      You use a Mock when it's an object that returns values that you set to the tested class. You use a Stub to mimic an Interface or Abstract class to be tested. In fact, the difference is very subtle and it doesn't really matter what you call it, fake, mock, or stub, they are all objects that aren't used in production, and used for managing complexity to write quality tests.

      Q. Can you list some of the key principles to remember while designing your classes?
      A. Use design principles and patterns, but use them judiciously. Design for current requirements without anticipating future requirements, and over complicating things. A well designed (i.e. loosely coupled and  more cohesive) system can easily lend itself to future changes, whilst keeping the system less complex.

      • Favor composition over inheritance.
      • Don't Repeat Yourself (DRY principle). Code once and only once.
      • Find what varies and encapsulate it.
      • Code to an interface, and not to an implementation.
      • Strive for loose coupling and high cohesion.

      Q. In your experience, what are some of the common dilemmas you face when writing unit tests?
      A. [Hint: provide the dilemma and the tips to overcome the dilemma]

      • Whether to fix the code or the test. When you write unit tests, sometimes you feel compelled to change your code just to facilitate the test. For example, when you need to test a private method or attribute. Doing so is a bad idea. If you ever feel tempted to make a private method public purely for testing purposes, don't do it. Testing is meant to improve the quality of your code, not decrease it. Having said this, in most cases, thinking about the test first in a TDD (.i.e. Test Driven Development) will help you refactor and write better code. Also, mock frameworks like PowerMock, let you mock private methods, static methods, constructors, final classes and methods, etc. Care must be taken in testing private methods. Inside a class you can end up with a lot of private methods manipulating instance or class variables without having them passed as parameters. This leads to high method coupling, and something not recommended. Instead use private methods that take explicit parameters and provide explicit return values.
      • Whether to use mocks or not. The quality unit tests need to be repeatable. Some non-deterministic or environmental conditions can make your code fragile. Mocking these scenarios can make your code more robust. For example, a CircusService class can use a mock CircusDao to avoid test cases failing due to data related issues. Alternatively, the test cases can use frameworks like DBUnit to insert data  during test set up and  remove data during the test tear down to provide stable data for the test cases. Some test cases rely on Web service calls, and availability of these services are non-deterministic, and it makes more sense to mock those services.

      • How to test non-functional requirements? For example, a non-functional requirement like 95 percent of all Web-based transactions should complete within 2 seconds. Your approach here would be to simply run the test code many times in a loop and compare the transaction times with target time, and keeping track of the number of passes and fails. If at the end of the test less than 95 percent of the transactions fail, then fail the test too. There are scenarios where @Test(timeout=5)  becomes handy to fail a test case that takes longer than 5 seconds to execute.

      • Testing multi-thread code. This can be quite challenging  and sometimes an impossible task. If the tests are too complex, then review your code and design. There are ways to program for multi-threading that are more conducive for testing. For example, making  your objects immutable where possible, reducing the number of instances where multiple threads interact with the same instance, and using the ExecutorService (e.g. having a SerialExecutorService which is part of the jconch framework to execute the threads serially)  instead of the Thread class, etc. There are frameworks like GroboUtils and MultithreadedTC aiming to expand the testing possibilities of Java with support for multi-threading and hierarchical unit testing. The goal of  jconch project is to provide safe set of implementations for common tasks in multi-threaded Java applications. The SerialExecutorService class is one of them.

      Related links:

      Labels: ,