Mocking exception using JMockit

In today’s post I would talk about mocking exceptions using JMockit. You would like to test exception scenarios, especially when you deal with third party services. This will help you to fine tune your code to deal with the exception. During the development time testing an exception scenario without the help of the second party/third party service developer is not an easy task. However a mocking framework like JMockit will make things very simple for us.

package com.ourownjava.tdd.mockit.exception;

import java.io.OutputStream;
import java.rmi.RemoteException;

/**
 * 
 * @author Sanju Thomas
 *
 */
public class FileService {

	private ThirdPartyService thirdPartyService;
	
	public void saveInRemoteRepository(final OutputStream outputStream) throws RemoteSaveFailedException{
		try {
			thirdPartyService.save(outputStream);
		} catch (RemoteException remoteException) {
			throw new RemoteSaveFailedException("Received Remote Exception from ThirdPartyService", remoteException);
		}
	}
	
}

package com.ourownjava.tdd.mockit.exception;

import java.io.OutputStream;
import java.rmi.RemoteException;

/**
 * 
 * @author Sanju Thomas
 *
 */
public interface ThirdPartyService {
	
	public void save(final OutputStream outputStream) throws RemoteException;

}

package com.ourownjava.tdd.mockit.exception;

import java.rmi.RemoteException;

/**
 * 
 * @author Sanju Thomas
 *
 */
public class RemoteSaveFailedException extends Exception{
	
	private static final long serialVersionUID = 1L;

	public RemoteSaveFailedException(final String message, final RemoteException remoteException){
		super(message, remoteException);
	}

}


package com.ourownjava.tdd.jmockit.exception;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;

import mockit.Deencapsulation;
import mockit.Injectable;
import mockit.NonStrictExpectations;
import mockit.Verifications;

import org.junit.Before;
import org.junit.Test;

import com.ourownjava.tdd.mockit.exception.FileService;
import com.ourownjava.tdd.mockit.exception.RemoteSaveFailedException;
import com.ourownjava.tdd.mockit.exception.ThirdPartyService;

/**
 * 
 * @author Sanju Thomas
 *
 */
public class TestFileService {
	
	private FileService fileService;
	
	@Injectable
	private ThirdPartyService thirdPartyService;
	
	@Before
	public void setUp(){
		fileService = new FileService();
		Deencapsulation.setField(fileService, "thirdPartyService", thirdPartyService);
	}
	
	@Test(expected = RemoteSaveFailedException.class)
	public void shouldHandleRemoteException() throws RemoteException, FileNotFoundException, RemoteSaveFailedException{
		
		new NonStrictExpectations() {{
			thirdPartyService.save((OutputStream) any);
			result = new RemoteException();
		}};
		
		fileService.saveInRemoteRepository(new FileOutputStream(new File("test.dat")));
		
		new Verifications() {{
			thirdPartyService.save((OutputStream) any);
		}};
	}

}

3 thoughts on “Mocking exception using JMockit

  1. how to write J Mock Exception for below code and please help me .

    public void testexecuteNegative()throws Exception {

    mockery.checking(new Expectations() { {
    allowing(mockHttpServletRequest).getParameter(“policyNumber”);
    will(returnValue(“gdscvsdg”));
    allowing(mockHttpServletRequest).getParameter(“zipCode”);
    will(returnValue(“12343″));
    }});

    ModelAndView modelAndView =propertyInfocontroller.execute(mockHttpServletRequest , mockHttpServletResponse );

    assertEquals(“executeLogin method of propertysearchController working fine to get view name “, “propertyAjaxResponse”,modelAndView.getViewName());

    }

Leave a comment

Your email address will not be published. Required fields are marked *