0

I am currently using EasyMock and creating mock objects like this

mockedObject = createMock(myInterface.class);

Easy mock is an unnecessary overhead(because the method that i mock is really simple) and i would like to create a mock without it.

But the problem is myInterface is an interface so how do i instantiate it.Please suggest.

Thanks, Sriram

5 Answers 5

4

The easiest way would be to create an inner class that implements the interface, implement the methods to return the data that you want, then use it in the test case.

For example:

public void testMethod ( )
{
    MyInterface mockObject = new MyInterface ( ) {
        public void myMethod ( )
        {
            // NOOP
        }

        public int myFunction ( )
        {
            return -1 ;
        }
    }

    // Proceed with the test case using mockObject
}
Sign up to request clarification or add additional context in comments.

3 Comments

It depends on whether you will ever reuse that particular implementation of the class again. In my case, many mock Objects started as anonymous inner classes and found themselves promoted to full fleged classes when I started reusing them.
@sriram Just to note for the record. Most of the explicitly created mock objects eventually end up disappearing and being replaced by EasyMock. Once a mock object reaches a certain level of complexity, maintaining it becomes more work than simply using EasyMock.
@Hagger yeah i know but my usage of mocking is very limited in this case so there is no need of additional overhead. I just have this just one function to mock.
1

You can create and Anoymous class e.g.

   MyInterface myMock = new MyInterface() {
                               ... methods implemented here
                            };

Comments

1

If you need to verify the number of times a method is called you can add a simple counter member for each method.

e.g.

public void testMethod ( )
{
    MyInterface mockObject = new MyInterface ( ) {
       public int MyMethodCount = 0;
       public int MyFunctionCount = 0;

        public void myMethod ( )
        {
            MyMethodCount++;
            // NOOP
        }

        public int myFunction ( )
        {
            MyFunctionCount++;
            return -1 ;
        }
    }

    // Proceed with the test case using mockObject
}

Comments

1

In the absence of a mock framework, java.lang.reflect.Proxy is your best bet. If you've never used it before, you can create a dynamic object which implements a set of interfaces, and you use the InvocationHandler to check each method call and decide what to do. This is a very powerful technique (and not limited to testing), as you can delegate method calls to other objects, etc.... It also insulates you from certain interface changes when you do this sort of delegation, as you don't declare each method. It adapts to the interface at runtine.

public static interface MyIntf {
  String foo(String arg1);
}

InvocationHandler invocationHandler = new InvocationHandler() {
   @Override
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
       if (method.getName().equals("foo") && 
           method.getParameterTypes().length == 1 &&
           method.getParameterTypes()[0] == String.class) {
           // do your mocking here.  For now i'll just return the input
           return args[0];
       }
       else {
           return null;
       }
   }
};

MyIntf myintf = (MyIntf) Proxy.newProxyInstance(getClass().getClassLoader(),
            new Class[] { MyIntf.class },
            invocationHandler);

System.out.println(myintf.foo("abc"));

1 Comment

this pattern is great
0

You cannot instantiate an interface, you need to work with one of its implementation. Since you have decided to forgo EasyMock (not sure why), then you need to either instantiate one of the existing interfaces implementations, or create a new one just for testing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.